Number Format (decimal,comma)

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
lemodizon
Posts: 218
Joined: Thu Apr 05, 2018 3:33 pm

Number Format (decimal,comma)

Post by lemodizon » Wed Jul 10, 2024 11:35 am

hi everyone,

Good day.

is there a simple way to format a number ex 279495 = 279,495.00? and when the user type a number ex 100 it will format automatic into 100.00
formatNumber.png
formatNumber.png (10.57 KiB) Viewed 2528 times

Code: Select all

on mouseUp
   GetTheTotalGallons
end mouseUp

command  GetTheTotalGallons
   put the dgText of grp "SbCustOrderList" of stack "SBCustOrdersList" into tData
   set itemDelimiter to tab
   
   repeat for each line tLine in tData
      if item 3 of tLine is a number  then
         add item 3 of tLine to tSumTotalGallon
      end if
      
      if item 6 of tLine is a number  then
         add item 6 of tLine to tSumTotalAmnt
      end if
      
   end repeat
   put tSumTotalGallon into fld "TotalGallons"
   put tSumTotalAmnt into fld "GrndTotalAmt"
end GetTheTotalGallons

thanks for the help in advance
Thank you & God Bless Everyone :wink:

Regards,
lemodizon

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Number Format (decimal,comma)

Post by dunbarx » Wed Jul 10, 2024 2:56 pm

Hi.

Code: Select all

on mouseUp
   ask "enter a number" with "12324567.89"
   if it is a number then
      set the itemDel to "."
      put item 1 of it into temp
      if item 2 of it <> "" then put "." & item 2 of it into decimal else put "" into decimal
      
      repeat with y = the number of chars of temp down to 1 step -3
         if y = the number of chars of temp then next repeat 
         put comma before char y + 1 of temp
      end repeat
      if char 1 of temp = comma then delete char 1 of temp
     answer temp &  decimal 
   end if
end mouseUp
Craig

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Number Format (decimal,comma)

Post by dunbarx » Wed Jul 10, 2024 4:49 pm

Hi again.

I did not see the thing about automatically formatting to two decimal places.

So are there never more than two in the initial number, that is, never "123.456"? And if only one, do you still want two? This is a dollars and cents thing?

Craig

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Number Format (decimal,comma)

Post by dunbarx » Wed Jul 10, 2024 5:24 pm

Assuming that this is a money thing, the following works:

Code: Select all

on mouseUp
   ask "enter a number" with "12324567.89"
   if it is a number then
      set the itemDel to "."
      put item 1 of it into temp
      put item 2 of it into decimal
      switch 
         case decimal = ""
            put ".00" into decimal
            break
         case the length of decimal = 1
            put "." & decimal & "0" into decimal
            break
         case the length of decimal = 2
            put "." & decimal  into decimal
            break
         case the length of decimal > 2
            put "." & char 1 to 2 of decimal into decimal
            break
      end switch
      
      repeat with y = the number of chars of temp down to 1 step -3
         if y = the number of chars of temp then next repeat 
         put comma before char y + 1 of temp
      end repeat
      
      if char 1 of temp = comma then delete char 1 of temp
      answer temp &  decimal 
   end if
end mouseUp
Craig

lemodizon
Posts: 218
Joined: Thu Apr 05, 2018 3:33 pm

Re: Number Format (decimal,comma)

Post by lemodizon » Thu Jul 11, 2024 1:55 am

dunbarx wrote:
Wed Jul 10, 2024 5:24 pm
Assuming that this is a money thing, the following works:

Code: Select all

on mouseUp
   ask "enter a number" with "12324567.89"
   if it is a number then
      set the itemDel to "."
      put item 1 of it into temp
      put item 2 of it into decimal
      switch 
         case decimal = ""
            put ".00" into decimal
            break
         case the length of decimal = 1
            put "." & decimal & "0" into decimal
            break
         case the length of decimal = 2
            put "." & decimal  into decimal
            break
         case the length of decimal > 2
            put "." & char 1 to 2 of decimal into decimal
            break
      end switch
      
      repeat with y = the number of chars of temp down to 1 step -3
         if y = the number of chars of temp then next repeat 
         put comma before char y + 1 of temp
      end repeat
      
      if char 1 of temp = comma then delete char 1 of temp
      answer temp &  decimal 
   end if
end mouseUp
Craig

Hi Craig,

Good day.

Thanks for sharing. I appreciate it.

Both are working and very simply to understand

I will review and study the code you gave for my reference in the future.

question i wonder if this possible to convert all the numbers below in my datagrid in decimal & comma.
formatNumber.png
formatNumber.png (11.29 KiB) Viewed 2448 times
there is decimal already coz in mysql i already setup the decimal but the comma i don't know how to setup in mysql

i tried to put with comma in my mysql manual input it display in my datagrid but when I tried to sum it all it doesn't sum but when removed the comma manual it will sum.

thanks in advance
Thank you & God Bless Everyone :wink:

Regards,
lemodizon

stam
Posts: 3061
Joined: Sun Jun 04, 2006 9:39 pm

Re: Number Format (decimal,comma)

Post by stam » Thu Jul 11, 2024 8:00 am

lemodizon wrote:
Thu Jul 11, 2024 1:55 am
i tried to put with comma in my mysql manual input it display in my datagrid but when I tried to sum it all it doesn't sum but when removed the comma manual it will sum.

thanks in advance

Yes, the comma is not a mathematical symbol in this context, it’s a convention to help people read large numbers. Programmatically it’s mixing a string with a decimal number and expecting to do correct math… won’t work.

Think of the comma as an user interface affectation.
This means you put it in when displaying to the user, but when using the number in calculations you replace it with empty, and re-apply when showing the result to the user…

Post Reply