Monday, August 26, 2019

You are not logged in.

**ame****Member**- Registered: 2011-06-28
- Posts: 178

Hi,

I need to pass via curl some "numeric" values that can contains decimal

So, the number 5.1 in Europe (dot is converted to comma) is converted by Applescript as 5,1 and the curl command fail

I found the standard addition command "localized text"

Seems that it works.

## Applescript:

set a to 5.1

set b to localized string a

return "5.1"

First question is: it is recommended or not to use it?

Any other method to convert in string a decimal number preserving dot for pass in shell script?

Second question is:

Big numbers (like bytes of a big file) once gutted, are converted as scientific notation

I found the number to text routine

http://www.macosxautomation.com/applesc … rt-02.html

But on Mountain Lion trying to convert number 1234567890 --> 1.23456789E+9 return me "1,2345678"

Seems in error. Any workaround or unique routine to solve both problem?

Ame

Offline

**ame****Member**- Registered: 2011-06-28
- Posts: 178

Hi,

I modified the script for European users that need to convert number with decimal or number expressed as scientific notation.

I modified the line with set y to the offset of...

This is useful for pass data to curl and populate DB with number that require decimal expressed with dot.

I also added the case when number has only decimal.

Anyway the command localized text seems work good.

## Applescript:

on number_to_string(this_number)

set this_number to this_number as string

if this_number contains "E+" then

set x to the offset of "," in this_number

set y to the offset of "+" in this_number

set z to the offset of "E" in this_number

set the decimal_adjust to characters (y - (length of this_number)) thru -1 of this_number as string as number

if x is not 0 then

set the first_part to characters 1 thru (x - 1) of this_number as string

else

set the first_part to ""

end if

set the second_part to characters (x + 1) thru (z - 1) of this_number as string

set the converted_number to the first_part

repeat with i from 1 to the decimal_adjust

try

set the converted_number to the converted_number & character i of the second_part

on error

set the converted_number to the converted_number & "0"

end try

end repeat

return the converted_number

else if this_number contains "," then

set AppleScript's text item delimiters to ","

set arrayObjects to text items of this_number

set AppleScript's text item delimiters to "."

set this_number to arrayObjects as string

set AppleScript's text item delimiters to ""

return this_number

else

return this_number

end if

end number_to_string

Offline

**scriptfactors****Member**- Registered: 2006-04-01
- Posts: 4

do this

on convertNumberToString(n)

"php -r 'echo sprintf(\"%d\",\"" & n & "\");'"

do shell script result

return result

end convertNumberToString

cheers ;-)

Offline

**CK****Member**- From:: UK
- Registered: 2018-11-04
- Posts: 97

Hopefully I'm understanding you correctly, and that you want to convert an AppleScript number into a string that uses a dot as its decimal separator, and converts scientific notation into full decimal notation.

I posted this trick very recently to another post on this forum, and one of the complaints cited was that it didn't use localised decimal separators, only ".". In your case, this sounds like it would be an advantage.

It also has the benefit of expanding scientific notation into full decimal notation, handling numbers up to the order of 10¹⁶ in magnitude (beyond which the result will remain expressed in scientific notation).

## Applescript:

set n to n as yards as string

where **n** is your input number that needs converted into string format.**Output:**

## Applescript:

5.1 --> "5.1"

1.23456789E+9 --> ""1234567890"

Offline