Developers and content creators—a complete solution for growing an audience directly.

Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

- Roku Community
- :
- Developers
- :
- Roku Developer Program
- :
- Format a float value with a specified number of de...

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

Highlighted
##

Hi, is there any possibility to round of a float/double value with a given amount of decimals. For example turn the number 1.27456 into 1.3?

I haven't found anything in the documentation (2.4 SDK release) to support this. Tried a workaround with string conversion, but this turns out to be hard since floats and doubles don't have the tostr() interface method.

Also, I would like to write this formatted float value as text on a roImageCanvas. But this does not seem to work either because of the mentioned tostr() limitation since the text attribute is a String value.

Is this not possible, or is there a specific way of doing it?

Any help on this topic is appreciated!

areskar

Level 7

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

10-14-2010
06:13 AM

Format a float value with a specified number of decimals?

I haven't found anything in the documentation (2.4 SDK release) to support this. Tried a workaround with string conversion, but this turns out to be hard since floats and doubles don't have the tostr() interface method.

Also, I would like to write this formatted float value as text on a roImageCanvas. But this does not seem to work either because of the mentioned tostr() limitation since the text attribute is a String value.

Is this not possible, or is there a specific way of doing it?

Any help on this topic is appreciated!

4 Replies

Highlighted
##

X = 1.27456

'Multiply X by 10^(number of decimal places you want).

X = X * 10

'(X = 12.7456)

'Add 0.5

X = X + 0.5

'(X = 13.2456)

'Now lop off the remaining, unwanted decimal places

X = fix(X) 'Fix is defined in the Brightscript reference manual of the 2.6 sdk (I haven't looked at 2.7 yet). It simply chops off all decimal places to the right of the decimal point.

'(X = 13)

Divide by 10^(number of decimal places you want).

X = X / 10

'X = 1.3

Also in the same document, the str(x) method appears to take a float and return a string.

X = str(X)

'(X = " 1.3")

gonzotek

Level 8

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

10-14-2010
08:41 AM

Re: Format a float value with a specified number of decimals

"areskar" wrote:

Hi, is there any possibility to round of a float/double value with a given amount of decimals. For example turn the number 1.27456 into 1.3?

I haven't found anything in the documentation (2.4 SDK release) to support this. Tried a workaround with string conversion, but this turns out to be hard since floats and doubles don't have the tostr() interface method.

Also, I would like to write this formatted float value as text on a roImageCanvas. But this does not seem to work either because of the mentioned tostr() limitation since the text attribute is a String value.

Is this not possible, or is there a specific way of doing it?

Any help on this topic is appreciated!

X = 1.27456

'Multiply X by 10^(number of decimal places you want).

X = X * 10

'(X = 12.7456)

'Add 0.5

X = X + 0.5

'(X = 13.2456)

'Now lop off the remaining, unwanted decimal places

X = fix(X) 'Fix is defined in the Brightscript reference manual of the 2.6 sdk (I haven't looked at 2.7 yet). It simply chops off all decimal places to the right of the decimal point.

'(X = 13)

Divide by 10^(number of decimal places you want).

X = X / 10

'X = 1.3

Also in the same document, the str(x) method appears to take a float and return a string.

X = str(X)

'(X = " 1.3")

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

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

Highlighted
##

areskar

Level 7

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

10-15-2010
12:27 AM

Re: Format a float value with a specified number of decimals

Thank you very much. This does the trick.

I totally missed the str() function! Perfect.

I totally missed the str() function! Perfect.

Highlighted
##

kraken

Level 8

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

a month ago

Re: Format a float value with a specified number of decimals

minutes = (Fix(length / .6) / 100 ). ToStr()

Will produce a number like 1.23

Highlighted
##

Tasevski

Level 8

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Thursday

Re: Format a float value with a specified number of decimals

I've made a small function for formatting an integer or float to have two digits after the decimal point:

function formatTwoDigitFloat(val as dynamic) as string if val = invalid then return "" formattedVal = Str(((val * 100) + 0.1) / 100) return Left(formattedVal, Len(formattedVal) - 1) end function print formatTwoDigitFloat(0) > 0.00 print formatTwoDigitFloat(5) > 5.00 print formatTwoDigitFloat(10) > 10.00 print formatTwoDigitFloat(5.6) > 5.60 print formatTwoDigitFloat(19.6) > 19.60 print formatTwoDigitFloat(19.99) > 19.99 print formatTwoDigitFloat(1.99) > 1.99

Can be modified to support more/less precise output if needed.