Burgs Posted February 25, 2015 Share Posted February 25, 2015 Greetings, This will probably be an easy question for somebody here, however I'm having a hard time with it. I have some calculations that output floating values, with 2 digits following the decimal point...for example .01 .02 .03 .10 .11 etc I'm using the 'stringformat' command to format them with the 2 digits, and include a "0" if a value requires it (for example .1 becomes .10 or .2 becomes .20)...that all works fine. I'm using the command as such StringFormat(%.2f, value) My problem is that my values have a "0" added BEFORE the decimal point, which I do NOT want...I'm having a problem trying to format with only a blank before the decimal. So my output appears as this: 0.01 0. 02 0.03 0.04 etc... however I would like it to appear as this: .01 .02 .03 .04 etc... I have tried "StringFormat(%0.2f, value), StringFormat(% .2f, value), StingFormat(% .2g, value)" and some others without success... any clues on what I'm doing wrong is greatly appreciated, I thank you in advance. Link to comment Share on other sites More sharing options...
jguinch Posted February 25, 2015 Share Posted February 25, 2015 (edited) I don't know how to do that with StringFormat, but here is a regex way : $string = ".1 .20 .3 .4 .50" $string = StringRegExpReplace($string, "\.\d\b\K", "0") ConsoleWrite($string) Edited February 25, 2015 by jguinch Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
Burgs Posted February 25, 2015 Author Share Posted February 25, 2015 hello, thanks for the reply, i'll take a look at that when I have a chance, but I was hoping to have an idea how to accomplish it with the StringFormat command, so as to prevent any serious re-keying...there must be a way I would think...? Link to comment Share on other sites More sharing options...
UEZ Posted February 25, 2015 Share Posted February 25, 2015 (edited) What about this?For $i = 0.1 To 1.1 Step 0.05 $fResult = $i < 1 ? StringTrimLeft(StringFormat("%.2f", $i), 1) : StringFormat("%.2f", $i) ConsoleWrite($fResult & @CRLF) NextBr,UEZ Edited February 25, 2015 by UEZ Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
Burgs Posted February 25, 2015 Author Share Posted February 25, 2015 hmmm that's an interesting idea...didn't think of simply trimming the left side if less than 1...thanks I will give that a try. Regards Link to comment Share on other sites More sharing options...
mikell Posted February 25, 2015 Share Posted February 25, 2015 For $i = 0.1 To 1.1 Step 0.05 $fResult = StringRegExpReplace(StringFormat("%.2f", $i), '^0', "") ConsoleWrite($fResult & @CRLF) Next Link to comment Share on other sites More sharing options...
zalomalo Posted February 25, 2015 Share Posted February 25, 2015 Never use spaces to separate values in a string, its a bad practice and you will save lots of bugs in future. Use any other ASCII char like , ; or even #. My english shucks, i know it. Link to comment Share on other sites More sharing options...
Burgs Posted February 26, 2015 Author Share Posted February 26, 2015 Thanks again to all who replied, gives me some good options... regards Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now