Jump to content

StringFormat() to return Decimal Only


Recommended Posts

  • Moderators

zeenmakr,

Try StringSplit on the decimal point.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

zeenmakr,

Quote

Trying to avoid all other alternatives

Why? If something works why refuse to use it?

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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

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

:)

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

 

 

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

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 by jchd

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

  • Recently Browsing   0 members

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