I need to do some formatting of numbers and it seems Brightscript is fairly limited in math functions. Does anyone have a good solution for formatting numbers to 2 decimal places? I’m pulling in numbers from external sources and they come in unformatted. For example: 160 or 160.9 or 160.95 or 160.955 or -160 etc. I need a to format as 160.95 or -160.95. I’m sure I could come up with some hack to do it, but there’s probably a better way than I’m coming up with.
TIA
Mark
Something like this ought to work:
v = Int((value + .005) * 100)
units = int(v/100)
decimal = v mod 100
d = decimal.ToStr()
if d < 10 then d = "0" + d
formatted = units.ToStr() + "." + d
I just wrote this off the top of my head and haven’t tested it.
–Mark
ok, I added this:
Int(lp) = content.lprice
to deal with the type mismatch of my initial value, but I’m also getting a type mismatch on this line that I’m not sure about:
if d < 10 then d = "0" + d
Is it the type of “10” that’s mismatched?
The code looks like:
Int(lp) = content.lprice
v = Int((lp + .005) * 100)
units = int(v/100)
decimal = v mod 100
d = decimal.ToStr()
if d < 10 then d = "0" + d
formatted = units.ToStr() + "." + d
print formatted
That should be
if decimal < 10 then d = "0" + d
–Mark
“agmark” wrote:
ok, I added this:Int(lp) = content.lpriceto deal with the type mismatch of my initial value, but I’m also getting a type mismatch on this line that I’m not sure about:
if d < 10 then d = "0" + dIs it the type of “10” that’s mismatched?
The code looks like:Int(lp) = content.lprice v = Int((lp + .005) * 100) units = int(v/100) decimal = v mod 100 d = decimal.ToStr() if d < 10 then d = "0" + d formatted = units.ToStr() + "." + d print formatted ```The line immediately above the one that has the mismatch error sets the type of d to String. Since you still have the numerical decimal variable, try
if decimal < 10 then d = "0" + d
/edit Mark beat me to it ![]()
Haha. Good eyes guys! Thanks for the quick help. I also had my initial value converted to an integer wrong.
Int(lp) = content.lprice
should be:
lp = content.lprice
lp = lp.ToInt()
Hm, I don’t understand that part. Why are you converting it to an integer? If you do that, the decimal part will always be .00
–Mark
You’re right. I’m getting a type mismatch and I thought that converting the inital string to an int would solve it, but as you say, now I’m getting zeros. How do I convert the initial value to something that this line can deal with?
v = Int((lp + .005) * 100)
My initial value from the xml parse is
content.lprice
assigned to lp so I could experiment with the value with:
lp = content.lprice
You probably want ToFloat
lp = content.lprice.ToFloat()
–Mark
Now that’s something I hadn’t even tried. It looks like it’s working on positive numbers, but I’m getting a weird format for negatives. I’m not sure why, but a negative number is formatting like: -1.0-63 whereas a postive is correct like: 0.07. Any clue why?
The easiest way to make it work with negatives is probably to check if the number is negative at the beginning. If so, make it positive and set a flag. Then run through the code you have, and at the end check the flag and if it’s set, prepend a minus sign to the result.
–Mark
Thanks Mark. Finally got back to check it out and you’re right, checking for a negative and setting a flag was the solution.
