Formatting numeric values
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
-
- Posts: 49
- Joined: Sat Nov 30, 2013 6:40 am
Formatting numeric values
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.
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.
-
- VIP Livecode Opensource Backer
- Posts: 3901
- Joined: Sat Mar 24, 2007 2:54 am
- Location: Palo Alto
Re: Formatting numeric values
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;
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
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
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!
-
- Posts: 49
- Joined: Sat Nov 30, 2013 6:40 am
Re: Formatting numeric values
Thank you Simon,
You just light up my day
because i just need a thousand separator from the function I modified your code become this:
now it will work on any number of digits and have separator option too.
Kind regard's
Saman Sjr.
You just light up my day
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
Kind regard's
Saman Sjr.
-
- VIP Livecode Opensource Backer
- Posts: 2262
- Joined: Thu Feb 28, 2013 11:52 pm
- Location: Göttingen, DE
Re: Formatting numeric values
..........
Last edited by [-hh] on Wed Aug 13, 2014 2:57 pm, edited 1 time in total.
shiftLock happens
-
- VIP Livecode Opensource Backer
- Posts: 3901
- Joined: Sat Mar 24, 2007 2:54 am
- Location: Palo Alto
Re: Formatting numeric values
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 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!
-
- VIP Livecode Opensource Backer
- Posts: 2262
- Joined: Thu Feb 28, 2013 11:52 pm
- Location: Göttingen, DE
Re: Formatting numeric values
..........
Last edited by [-hh] on Wed Aug 13, 2014 2:57 pm, edited 1 time in total.
shiftLock happens
-
- Posts: 32
- Joined: Sun Jun 22, 2014 2:37 am
Re: Formatting numeric values
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:
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.
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