Jump to content

FORMAT NUMBER WHILE PRINTING TO A TEXT FILE


Rskm
 Share

Recommended Posts

Hi, i have a variable (floating number - could be positive or negative), i wish to print it into a text file, how can i keep the format in such a way that it always has 4 digits ahead of decimal and 2 after decimal

basically i have total 7 columns in the text file to print the variable  ;

eg: variable = 1.235, output requied = 0001.24

variable=-23.55555, output required =-023.56

Link to comment
Share on other sites

7 minutes ago, Rskm said:

01234.57

That's not what you yourself wanted!

1 hour ago, Rskm said:

it always has 4 digits ahead of decimal and 2 after decimal

 

1 hour ago, Rskm said:

basically i have total 7 columns in the text file to print the variable

The output 01234.57 needs 8 "columns".

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

@jchd, i was taking a simple example with stringformat. All i want is that the cells should be occupied with zero if sufficient digits are not actually present in the number.. 

 

assuming 7 columns dedicated for printing, i expect 123.568 to be printed as 0123.57 and -1.2 to be printed as -001.20

Link to comment
Share on other sites

This is the widest span you can process with 7 print positions:

Local $aPis = [ _
    -314.15926, _
     -31.415926, _
      -3.1415926, _
      -0.31415926, _
      -0.031415926, _
      -0.0031415926, _
      -0.00031415926, _
      -0.000031415926, _
       0.000031415926, _
       0.00031415926, _
       0.0031415926, _
       0.031415926, _
       0.31415926, _
       3.1415926, _
      31.415926, _
     314.15926, _
    3141.5926 _
]

For $f In $aPis
    ConsoleWrite(StringFormat("%07.2f", $f) & @LF)
Next

 

Edited by jchd
Mising required comma, sorry

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

"Combining" this way doesn't work correctly and often produces wrong output:

Local $aPis = [ _
    -314.15926, _
     -31.415926, _
      -3.1415926, _
      -0.31415926, _
      -0.031415926, _
      -0.0031415926, _
      -0.00031415926, _
      -0.000031415926, _
       0.000031415926, _
       0.00031415926, _
       0.0031415926, _
       0.031415926, _
       0.31415926, _
       3.1415926, _
      31.415926, _
     314.15926, _
    3141.5926 _
]

For $f In $aPis
    ConsoleWrite(StringFormat("%07.2f", $f) & " =?= " & StringFormat("%04d", $f ) & StringRight(Round($f - Int($f),2),3) & @LF)
Next

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

You didn't:

-314.16 =?= -314.16
-031.42 =?= -031.42
-003.14 =?= -003.14
-000.31 =?= 0000.31    Wrong
-000.03 =?= 0000.03    Wrong
-000.00 =?= 0000-0     Non-sensical & format violation
-000.00 =?= 0000-0     Non-sensical & format violation
-000.00 =?= 0000-0     Non-sensical & format violation
0000.00 =?= 00000      Format violation
0000.00 =?= 00000      Format violation
0000.00 =?= 00000      Format violation
0000.03 =?= 0000.03
0000.31 =?= 0000.31
0003.14 =?= 0003.14
0031.42 =?= 0031.42
0314.16 =?= 0314.16
3141.59 =?= 3141.59

I still don't get how a much more convoluted approach using several function calls and conditionals is better than a single plain StringFormat.

Anyway feel free to post your fixed alternative solution.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Depending what Rskm wants in his text file?

Local $aPis = [ _
         - 314.15926, _
         - 31.415926, _
         - 3.1415926, _
         - 0.31415926, _
         - 0.031415926, _
         - 0.0031415926, _
         - 0.00031415926, _
         - 0.000031415926, _
        0.000031415926, _
        0.00031415926, _
        0.0031415926, _
        0.031415926, _
        0.31415926, _
        3.1415926, _
        31.415926, _
        314.15926, _
        3141.5926 _
        ]

For $f In $aPis
    If Round($f, 2) = 0 Then
        ConsoleWrite("00000.00" & @lf)
    Else
        ConsoleWrite(StringFormat("%05d", $f) & StringRight(Round($f - Int($f), 2), 3) & @LF)
    EndIf
Next

 

Link to comment
Share on other sites

He said he need to fill a 7-char field with always 2 decimals.

Your code still fails on integers:

Local $aPis = [ _
    -314.15926, _
     -31.415926, _
      -3.1415926, _
      -0.31415926, _
      -0.031415926, _
      -0.0031415926, _
      -0.00031415926, _
      -0.000031415926, _
       0.000031415926, _
       0.00031415926, _
       0.0031415926, _
       0.031415926, _
       0.31415926, _
       3.1415926, _
      31.415926, _
     314.15926, _
    3141.5926, _
    -1234, _
    -123, _
     -12, _
      -1, _
       1, _
      12, _
     123, _
    1234, _
    12345 _
]

For $f In $aPis
    If Round($f, 2) = 0 Then
        ConsoleWrite("00000.00" & @lf)
    Else
        ConsoleWrite(StringFormat("%05d", $f) & StringRight(Round($f - Int($f), 2), 3) & @LF)
    EndIf
Next

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Yes I saw the 7 and also your comment on 8 columns.

But any way, an even more convoluted approach?

Local $aPis = [ _
         - 314.15926, _
         - 31.415926, _
         - 3.1415926, _
         - 0.31415926, _
         - 0.031415926, _
         - 0.0031415926, _
         - 0.00031415926, _
         - 0.000031415926, _
        0.000031415926, _
        0.00031415926, _
        0.0031415926, _
        0.031415926, _
        0.31415926, _
        3.1415926, _
        31.415926, _
        314.15926, _
        3141.5926, _
         - 1234, _
         - 123, _
         - 12, _
         - 1, _
        1, _
        12, _
        123, _
        1234, _
        12345 _
        ]

For $f In $aPis
    If Round($f, 2) = 0 Then
        ConsoleWrite("00000.00" & @LF)
    Else
        ConsoleWrite(StringFormat("%05d", $f) & StringRight(StringFormat("%.2f", $F), 3) & @LF)
    EndIf
Next

 

Link to comment
Share on other sites

That was my point: in such cases, StringFormat is both more robust, versatile and less error-prone than a more pedestrian approach.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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

×
×
  • Create New...