Rskm Posted August 3, 2018 Posted August 3, 2018 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
UEZ Posted August 3, 2018 Posted August 3, 2018 Check out StringFormat 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!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ
Rskm Posted August 3, 2018 Author Posted August 3, 2018 Hi , i am not able to fill in '0's ahead.. $num=1234.5656565 stringformat("%5.2f",$num) gives output as 1234.57, i need the result as 01234.57
jchd Posted August 3, 2018 Posted August 3, 2018 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 hereRegExp tutorial: enough to get startedPCRE 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)
Rskm Posted August 3, 2018 Author Posted August 3, 2018 @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
jchd Posted August 3, 2018 Posted August 3, 2018 (edited) 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 August 3, 2018 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 hereRegExp tutorial: enough to get startedPCRE 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)
JoHanatCent Posted August 3, 2018 Posted August 3, 2018 You can combine the result. Something like this? $Start= 1234.5656565 MsgBox(4096,"",StringFormat("%05d", $Start ) & stringright(round($start - int($Start),2),3))
jchd Posted August 3, 2018 Posted August 3, 2018 "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 hereRegExp tutorial: enough to get startedPCRE 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)
JoHanatCent Posted August 4, 2018 Posted August 4, 2018 It looks pretty good If you us an if to solve the Zero answers?
jchd Posted August 4, 2018 Posted August 4, 2018 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 hereRegExp tutorial: enough to get startedPCRE 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)
JoHanatCent Posted August 6, 2018 Posted August 6, 2018 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
jchd Posted August 6, 2018 Posted August 6, 2018 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 hereRegExp tutorial: enough to get startedPCRE 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)
JoHanatCent Posted August 6, 2018 Posted August 6, 2018 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
jchd Posted August 6, 2018 Posted August 6, 2018 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 hereRegExp tutorial: enough to get startedPCRE 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)
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