Formatting numeric values

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
Saman Sjr.
Posts: 49
Joined: Sat Nov 30, 2013 6:40 am

Formatting numeric values

Post by Saman Sjr. » Thu Aug 07, 2014 5:44 am

Dear All,
in visual basic there is a function to display the numeric values in various forms.
for example : Format(1234567,”#,###”) will output "1,234,567"

how do i do this function in LC ?

regard's
Saman Sjr.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Formatting numeric values

Post by Simon » Thu Aug 07, 2014 7:25 am

Hi Saman Sjr.,
You have an opportunity to become a liveCode Hero here.
Write and publish a function.
I'll get you started with a very poor example;

Code: Select all

on mouseUp
   put numFormat(1234567,"#,###,###") into tNum
answer tNum
end mouseUp

function numFormat pNum,pFormat
   put 1 into y
   repeat with x = 1 to the number of chars in pFormat
      if char x in pFormat = "#" then
         put char y of pNum into char x of pFormat
         add 1 to y
      end if
   end repeat
   return pFormat
end numFormat
Now that satisfies your example but only that.
Can you figure out a way to make it work with any number of digits? And I think in some parts of Europe they use "." instead of "," as a separator.

Fun stuff!

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Saman Sjr.
Posts: 49
Joined: Sat Nov 30, 2013 6:40 am

Re: Formatting numeric values

Post by Saman Sjr. » Thu Aug 07, 2014 9:51 am

Thank you Simon,
You just light up my day :idea:
because i just need a thousand separator from the function I modified your code become this:

Code: Select all

    
on mouseUp
   put numFormat(1234567,",") into tNum
   answer tNum
end mouseUp

function numFormat pNum,pSeparator
   put the number of chars in pNum -3 into pNum_length
   put 0 into separator_counter
   repeat with separator_position = pNum_length down to 3 step -3
      add 1 to separator_counter
      put char 1 to separator_position of pNum into  left_part_of_pNum
      put char separator_position+1 to pNum_length+3+separator_counter of pNum into right_part_of_pNum
      put left_part_of_pNum & pSeparator & right_part_of_pNum into pNum
   end repeat
   return pNum
end numFormat
now it will work on any number of digits and have separator option too.

Kind regard's
Saman Sjr.

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Formatting numeric values

Post by [-hh] » Thu Aug 07, 2014 9:54 am

..........
Last edited by [-hh] on Wed Aug 13, 2014 2:57 pm, edited 1 time in total.
shiftLock happens

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Formatting numeric values

Post by Simon » Thu Aug 07, 2014 6:39 pm

Hi Hermann,
I have no idea about a local numerformat but it's a good idea.
I've seen many posts asking for dd/mm/yyyy date format.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Formatting numeric values

Post by [-hh] » Thu Aug 07, 2014 6:46 pm

..........
Last edited by [-hh] on Wed Aug 13, 2014 2:57 pm, edited 1 time in total.
shiftLock happens

JackieBlue
Posts: 32
Joined: Sun Jun 22, 2014 2:37 am

Re: Formatting numeric values

Post by JackieBlue » Fri Aug 08, 2014 3:54 am

I felt the same way you do about formatting numbers, I had to look a bit.

Did you look at the Format function? Here is what I did to print a dollar amount:

Code: Select all

   put "$" & format("%.2f",rightPrice) into field lblRightPRice
Luckily I was familiar with this (sort of) due to coding in C a long time ago. C uses a similar format with the "%.2f" when printing to screen (printf() function). This gives me a dollar amount that looks like "$3.99". I suppose a function could be written that would return formats of various types. I am just starting in LiveCode so unless I am finding a constant need I am not going to attempt it at this time.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”