zeenmakr Posted May 31, 2020 Share Posted May 31, 2020 Couldn't work out how to return 1415 from 3.1415 with StringFormat() trying to avoid StringRegExpReplace() or StringInStr(). Thought'd be as simple as "%d" flag for decimal but not it. https://www.autoitscript.com/autoit3/docs/functions/StringFormat.htm Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted May 31, 2020 Moderators Share Posted May 31, 2020 zeenmakr, Try StringSplit on the decimal point. M23 zeenmakr 1 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
zeenmakr Posted May 31, 2020 Author Share Posted May 31, 2020 1 minute ago, Melba23 said: zeenmakr, Try StringSplit on the decimal point. M23 yea forgot to mention, is possible with StringFormat(). Trying to avoid all other alternatives Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted May 31, 2020 Moderators Share Posted May 31, 2020 zeenmakr, Quote Trying to avoid all other alternatives Why? If something works why refuse to use it? M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
zeenmakr Posted May 31, 2020 Author Share Posted May 31, 2020 1 minute ago, Melba23 said: Why? If something works why refuse to use it? it's not that, want to gain new method. after all it is StringFormat, with "%01i" flag for interger and "%.4f" flag is pretty close but it doesn't eliminate the integer Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted June 1, 2020 Share Posted June 1, 2020 @zeenmakr As stated in the Help file about StringFormat function: Quote Width Specification The second optional field of the format specification is the width specification. The width argument is a nonnegative decimal integer controlling the minimum number of characters printed. If the number of characters in the output value is less than the specified width, blanks are added to the left or the right of the values — depending on whether the – flag (for left alignment) is specified — until the minimum width is reached. If width is prefixed with 0, zeros are added until the minimum width is reached (not useful for left-aligned numbers).The width specification never causes a value to be truncated. If the number of characters in the output value is greater than the specified width, or if width is not given, all characters of the value are printed (subject to the precision specification). So, you may use one of the various alternatives with all the others String* functions: #include <StringConstants.au3> Global $strNumber = "3.1415" ConsoleWrite("Method 1: " & StringRight($strNumber, StringLen($strNumber) - StringInStr($strNumber, ".")) & @CRLF) ConsoleWrite("Method 2: " & StringMid($strNumber, StringInStr($strNumber, ".") + 1) & @CRLF) ConsoleWrite("Method 3: " & StringSplit($strNumber, ".", $STR_NOCOUNT)[1] & @CRLF) ConsoleWrite("Method 4: " & StringRegExp($strNumber, '^\d+\.(\d*)$', $STR_REGEXPARRAYMATCH)[0] & @CRLF) ConsoleWrite("Method 5: " & StringRegExpReplace($strNumber, '^\d+\.(\d*)$', '$1') & @CRLF) zeenmakr 1 Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted June 1, 2020 Moderators Share Posted June 1, 2020 zeenmakr, Are you saying you want a new functionality for StringFormat which returns only the decimal part of a number? If so then it will never happen as StringFormat (as the name suggests) is about formatting an existing number, not extracting parts of it. And before you bring up the "%**i" method, that is designed to pad out integers with leading zeroes, not to extract the integer value (Int does that quite nicely), as explained in the Help file: The precision specifies the minimum number of digits to be printed. If the number of digits in the argument is less than precision, the output value is padded on the left with zeros. The value is not truncated when the number of digits exceeds precision. So just accept that you will need to use another function to achieve your aim - you have been shown many possibilities above. M23 zeenmakr and seadoggie01 2 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
zeenmakr Posted June 1, 2020 Author Share Posted June 1, 2020 3 hours ago, FrancescoDiMuro said: As stated in the Help file about StringFormat function: ... so, you may use one of the various alternatives with all the others String* functions: thanks for the info and appreciated the examples. any chance to return 0 if number has no decimal points 1 hour ago, Melba23 said: it will never happen as StringFormat (as the name suggests) is about formatting an existing number yea i was hoping, now that make sense. thanks for the info 1 hour ago, Melba23 said: the "%**i" method, that is designed to pad out integers with leading zeroes it does ignore the decimals which was how i got the wrong impression Link to comment Share on other sites More sharing options...
jchd Posted June 1, 2020 Share Posted June 1, 2020 (edited) 1 hour ago, zeenmakr said: it does ignore the decimals which was how i got the wrong impression It doesn't ignore anything, on the contrary. Under the hood, AutoIt parses the format string and since the type specifier is i (integer) it automagically invokes Int(argument) for you. Contrary to C with printf(), AutoIt is smart enough to implicitely perform necessary conversions for you. StringFormat("%f", 6) yields 6 after converting 6 to a double StringFormat("%f", ".3zz") yields 0.300000 after converting the string ".3zz" to a double. StringFormat("%12i", "-.5abc") yields 0 after converting the string "-.5abc" to an int. In C the first and second calls would likely produce garbage or core dump because 6 would be an integer (typically 32 bit), shorter than a double in memory (64 bit), causing an exception or converting data beyond the actual int boundary. The third call would definitely yield garbage. Edited June 1, 2020 by jchd zeenmakr 1 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) 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