topic Re: Format a float value with a specified number of decimals in Roku Developer Program
https://community.roku.com/t5/Roku-Developer-Program/Format-a-float-value-with-a-specified-number-of-decimals/m-p/137782#M1615
Thank you very much. This does the trick.<BR />I totally missed the str() function! Perfect.Fri, 15 Oct 2010 07:27:47 GMTareskar2010-10-15T07:27:47ZFormat a float value with a specified number of decimals?
https://community.roku.com/t5/Roku-Developer-Program/Format-a-float-value-with-a-specified-number-of-decimals/m-p/137780#M1613
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?<BR />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.<BR /><BR />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. <BR />Is this not possible, or is there a specific way of doing it?<BR /><BR />Any help on this topic is appreciated!Thu, 14 Oct 2010 13:13:21 GMThttps://community.roku.com/t5/Roku-Developer-Program/Format-a-float-value-with-a-specified-number-of-decimals/m-p/137780#M1613areskar2010-10-14T13:13:21ZRe: Format a float value with a specified number of decimals
https://community.roku.com/t5/Roku-Developer-Program/Format-a-float-value-with-a-specified-number-of-decimals/m-p/137781#M1614
<BLOCKQUOTE>"areskar" wrote:<BR />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?<BR />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.<BR /><BR />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. <BR />Is this not possible, or is there a specific way of doing it?<BR /><BR />Any help on this topic is appreciated!</BLOCKQUOTE><BR />X = 1.27456<BR />'Multiply X by 10^(number of decimal places you want).<BR />X = X * 10<BR />'(X = 12.7456)<BR />'Add 0.5<BR />X = X + 0.5<BR />'(X = 13.2456)<BR />'Now lop off the remaining, unwanted decimal places<BR />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.<BR />'(X = 13)<BR />Divide by 10^(number of decimal places you want).<BR />X = X / 10<BR />'X = 1.3<BR /><BR />Also in the same document, the str(x) method appears to take a float and return a string.<BR />X = str(X)<BR />'(X = " 1.3")Thu, 14 Oct 2010 15:41:37 GMThttps://community.roku.com/t5/Roku-Developer-Program/Format-a-float-value-with-a-specified-number-of-decimals/m-p/137781#M1614gonzotek2010-10-14T15:41:37ZRe: Format a float value with a specified number of decimals
https://community.roku.com/t5/Roku-Developer-Program/Format-a-float-value-with-a-specified-number-of-decimals/m-p/137782#M1615
Thank you very much. This does the trick.<BR />I totally missed the str() function! Perfect.Fri, 15 Oct 2010 07:27:47 GMThttps://community.roku.com/t5/Roku-Developer-Program/Format-a-float-value-with-a-specified-number-of-decimals/m-p/137782#M1615areskar2010-10-15T07:27:47ZRe: Format a float value with a specified number of decimals
https://community.roku.com/t5/Roku-Developer-Program/Format-a-float-value-with-a-specified-number-of-decimals/m-p/604889#M47967
<P>minutes = (Fix(length / .6) / 100 ). ToStr() </P><P>Will produce a number like 1.23 </P>Sun, 20 Sep 2020 01:03:54 GMThttps://community.roku.com/t5/Roku-Developer-Program/Format-a-float-value-with-a-specified-number-of-decimals/m-p/604889#M47967kraken2020-09-20T01:03:54ZRe: Format a float value with a specified number of decimals
https://community.roku.com/t5/Roku-Developer-Program/Format-a-float-value-with-a-specified-number-of-decimals/m-p/613141#M48120
<P>I've made a small function for formatting an integer or float to have two digits after the decimal point:<BR /><BR /></P><PRE>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</PRE><P>Can be modified to support more/less precise output if needed.</P>Thu, 15 Oct 2020 07:06:36 GMThttps://community.roku.com/t5/Roku-Developer-Program/Format-a-float-value-with-a-specified-number-of-decimals/m-p/613141#M48120Tasevski2020-10-15T07:06:36Z