Page 1 of 1

Formatting numeric values

Posted: Thu Aug 07, 2014 5:44 am
by Saman Sjr.
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.

Re: Formatting numeric values

Posted: Thu Aug 07, 2014 7:25 am
by Simon
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

Re: Formatting numeric values

Posted: Thu Aug 07, 2014 9:51 am
by Saman Sjr.
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.

Re: Formatting numeric values

Posted: Thu Aug 07, 2014 9:54 am
by [-hh]
..........

Re: Formatting numeric values

Posted: Thu Aug 07, 2014 6:39 pm
by Simon
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

Re: Formatting numeric values

Posted: Thu Aug 07, 2014 6:46 pm
by [-hh]
..........

Re: Formatting numeric values

Posted: Fri Aug 08, 2014 3:54 am
by JackieBlue
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.