Jump to content

stringformat 0 padding on both sides of floating point number


kylomas
 Share

Recommended Posts

Hi,

I am trying to format duration output as:

nnnn.nnnn

with 0 padding using stringformat.

I cannot get digits before the decimal to o pad. Ive tried the following

stringformat("%04.4f",round(timerdiff($time)/1000,4))
stringformat("%4.4f",round(timerdiff($time)/1000,4))
stringformat("%-4.4f",round(timerdiff($time)/1000,4))
stringformat("%-04.4f",round(timerdiff($time)/1000,4))

These all yield either n.nnnn or an error.

Perhaps one of you "C" gurus who understand "sprintf" know whether or not this can be done with stringformat, and how.

Alternatively, I'll put in code to determine the length of the lefthand part of the number and concatenate the correct number of zeros, a kludgy solution, at best.

Thanks,

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

AZJIO,

Thanks, was working on splitting the exponent from the mantissa and formatting each seperately. Your example does this nicely. I finally wound up with this

func c_timer_output($str,$time)
 local $ttmp = round(timerdiff($time)/1000,4)
 local $a10 = stringsplit($ttmp,".")
 consolewrite("!> " & stringformat("%-50s", $str) & " = " & stringformat("%0.4i.%.4i",$a10[1],$a10[2]) & " seconds" & @lf)
endfunc

Even though it works, it seems like a goofy solution. Perhaps I'm being naive!

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

You have to count all characters for the minimum length. 4 digits + 1 decimal mark + 4 digits = 9 chars

stringformat("%09.4f",round(timerdiff($time)/1000,4))

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...