Number formatting

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
simon.schvartzman
Posts: 665
Joined: Tue Jul 29, 2014 12:52 am

Number formatting

Post by simon.schvartzman » Sun May 15, 2016 9:07 pm

Hi team, I'm at the very begining of my learning curve with LC and therefore please excuse me for this basic question. As a disclaimer before posting I spen a considerable amount of time searching on all available documentation and wasn't able to find the answer...

Assuming on a given field I have a number 12345678912 and when closing the field I want it to look like this: 123.456.789,12 (123,456,789.12 will also work).

How should I do it?

Many thanks in advance...
Simon
________________________________________
To ";" or not to ";" that is the question

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Number formatting

Post by jmburnod » Mon May 16, 2016 10:18 am

Hi Simon,
Not sure that is the best way but this script do it

Code: Select all

on mouseUp
   put "12345678912" into t0
   put empty into t1
   put "," & char -2 to -1 of t0 into t1
   put char -5 to -3 of t0  before t1 
   put char -8 to -6 of t0 & "." before t1 
   put char -11 to -9 of t0 & "."  before t1 
   answer t1   & cr & "123.456.789,12 expected"
end mouseUp
Best regards
Jean-Marc
https://alternatic.ch

simon.schvartzman
Posts: 665
Joined: Tue Jul 29, 2014 12:52 am

Re: Number formatting

Post by simon.schvartzman » Mon May 16, 2016 11:51 am

Jean-Marc, many thanks it works as expected!

Best!
Simon
________________________________________
To ";" or not to ";" that is the question

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Number formatting

Post by jmburnod » Mon May 16, 2016 2:02 pm

Great,
but I guess you need the same for other numbers.
Function return a value and it is a good way
Something like this

Code: Select all

on mouseUp
   put setMynumber(12345678912)-- the result to message box
end mouseUp

function setMynumber pNumber
    put pNumber into t0
   put empty into t1
   put "," & char -2 to -1 of t0 into t1
   put char -5 to -3 of t0  before t1 
   put char -8 to -6 of t0 & "." before t1 
   put char -11 to -9 of t0 & "."  before t1 
   return t1
end setMynumber
All the best
Jean-Marc
https://alternatic.ch

Post Reply