Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
agmark
Visitor

Math Functions

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
0 Kudos
11 REPLIES 11
RokuMarkn
Visitor

Re: Math Functions

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
0 Kudos
agmark
Visitor

Re: Math Functions

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
0 Kudos
RokuMarkn
Visitor

Re: Math Functions

That should be

if decimal < 10 then d = "0" + d


--Mark
0 Kudos
gonzotek
Visitor

Re: Math Functions

"agmark" wrote:
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
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 🙂
Remoku.tv - A free web app for Roku Remote Control!
Want to control your Roku from nearly any phone, computer or tablet? Get started at http://help.remoku.tv
by Apps4TV - Applications for television and beyond: http://www.apps4tv.com
0 Kudos
agmark
Visitor

Re: Math Functions

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()
0 Kudos
RokuMarkn
Visitor

Re: Math Functions

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
0 Kudos
agmark
Visitor

Re: Math Functions

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
0 Kudos
RokuMarkn
Visitor

Re: Math Functions

You probably want ToFloat

lp = content.lprice.ToFloat()


--Mark
0 Kudos
agmark
Visitor

Re: Math Functions

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?
0 Kudos