Jump to content

Recommended Posts

  • Moderators
Posted

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:

  Reveal hidden contents

 

  • Moderators
Posted

zeenmakr,

  Quote

Trying to avoid all other alternatives

Expand  

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:

  Reveal hidden contents

 

Posted
  On 5/31/2020 at 5:35 PM, Melba23 said:

Why? If something works why refuse to use it?

Expand  

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

Posted

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

Expand  

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:

  Reveal hidden contents

 

  • Moderators
Posted

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:

  Reveal hidden contents

 

Posted

 

 

  On 6/1/2020 at 6:38 AM, 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:

Expand  

thanks for the info and appreciated the examples. any chance to return 0 if number has no decimal points

  On 6/1/2020 at 8:06 AM, Melba23 said:

it will never happen as StringFormat (as the name suggests) is about formatting an existing number

Expand  

yea i was hoping, now that make sense. thanks for the info

  On 6/1/2020 at 8:06 AM, Melba23 said:

the "%**i" method, that is designed to pad out integers with leading zeroes

Expand  

it does ignore the decimals which was how i got the wrong impression

Posted (edited)
  On 6/1/2020 at 10:08 AM, zeenmakr said:

it does ignore the decimals which was how i got the wrong impression

Expand  

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
  Reveal hidden contents

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)

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
  • Recently Browsing   0 members

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