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: 
GrantM07
Reel Rookie

How do i convert a float that comes out as scientific notation to a regular number?

Jump to solution

Whenever i attempt to print a float i get 7.597e+09. Thats without to string. And when i try to use .ToStr() i still get 7.597e+09. I dont know how to correct this.

0 Kudos
1 Solution

Accepted Solutions
RokuBen
Community Moderator
Community Moderator

Re: How do i convert a float that comes out as scientific notation to a regular number?

Jump to solution

There's an undocumented Format method on strings that can be used like this.  The string has a C-like printf format string, and the Format method takes the arguments and applies them to the % parameters.

Brightscript Debugger> ? 7.957e+09
 7.957e+09

Brightscript Debugger> ? "%2.5f".Format(7.97e+9)
7969999872.00000

 

View solution in original post

0 Kudos
2 REPLIES 2
RokuBen
Community Moderator
Community Moderator

Re: How do i convert a float that comes out as scientific notation to a regular number?

Jump to solution

There's an undocumented Format method on strings that can be used like this.  The string has a C-like printf format string, and the Format method takes the arguments and applies them to the % parameters.

Brightscript Debugger> ? 7.957e+09
 7.957e+09

Brightscript Debugger> ? "%2.5f".Format(7.97e+9)
7969999872.00000

 

0 Kudos
RokuKC
Roku Employee
Roku Employee

Re: How do i convert a float that comes out as scientific notation to a regular number?

Jump to solution

@RokuBen wrote:

There's an undocumented Format method on strings that can be used like this.  The string has a C-like printf format string, and the Format method takes the arguments and applies them to the % parameters.

 

Brightscript Debugger> ? 7.957e+09
 7.957e+09

Brightscript Debugger> ? "%2.5f".Format(7.97e+9)
7969999872.00000

 

 


I'm not sure if it was intentional to use a different value here for the example, but it confused me at first read. 🙂
With the original value I see:

Brightscript Debugger> x = 7.957e+09
Brightscript Debugger> ? "%2.5f".Format(x)
7957000192.00000

I assume that the extra 192 is due to limitation of Float precision.
If you use a Double value, which can be created using the '#' type suffix, it doesn't show that issue.

Brightscript Debugger> x = 7.957e+09#
Brightscript Debugger> ? "%2.5f".Format(x)
7597000000.00000

Also note, instead of using <string>.Format(), you can pass the format specified to <floatingPoint>.ToStr() directly.

Brightscript Debugger> x = 7.957e+09
Brightscript Debugger> ? x.ToStr("%2.5f")
7957000192.00000

Brightscript Debugger> x = 7.957e+09#
Brightscript Debugger> ? x.ToStr("%2.5f")
7597000000.00000

 

0 Kudos