Sign in to follow this  
Followers 0
Burgs

blank before decimal?

8 posts in this topic

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. 

Share this post


Link to post
Share on other sites



#2 ·  Posted (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 by jguinch

Share this post


Link to post
Share on other sites

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...?

Share this post


Link to post
Share on other sites

#4 ·  Posted (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)
Next
Br,

UEZ

Edited 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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

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  :)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Thanks again to all who replied, gives me some good options...   regards  :)

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0