• Post Reply Bookmark Topic Watch Topic
  • New Topic

Prevent Scientific Notation On a Calculation  RSS feed

 
Steve Dyke
Ranch Hand
Posts: 1779
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following calculation:



It returns :
1.0047670225E10

Is there any way to prevent the Scientific Notation?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66263
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you printing it out?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16049
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scientific notation is just a way to write numbers. It has nothing to do with the calculation, or even with the number itself. In other words, there is no such thing as "preventing scientific notation on a calculation".

Whether a number is displayed in scientific notation or not, depends on how you let Java format and print the number. The easiest way to print a double value in "normal" notation is like this:

 
Stephan van Hulst
Saloon Keeper
Posts: 7932
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also note that changing the number of decimals you use in a double literal won't change the precision of it. Floating point numbers have a fixed precision. If you want arbitrary precision, use BigDecimal.
 
Steve Dyke
Ranch Hand
Posts: 1779
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:How are you printing it out?


I am trying to use it in an SQL string to add a record to a remote data base.

I got the scientific natation from the console as part of the message I got when it failed. I need to save it as a 10 digit integer. The number in the calculation is a 13 digit number(just trying to shrink it down to 10 digits).
 
Stephan van Hulst
Saloon Keeper
Posts: 7932
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it's an integer, why are you using double?

Maybe you can explain exactly what the number is supposed to represent and what the calculation is supposed to do.
 
Steve Dyke
Ranch Hand
Posts: 1779
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:If it's an integer, why are you using double?

Maybe you can explain exactly what the number is supposed to represent and what the calculation is supposed to do.


The following code generates a long number of 13 digits:



My remote data field is an integer type that holds 10 digits.

If I subtract 1490000000000 from the long number I get a 10 digit number.

However, when I use the result in my SQL string to insert the record the number is represented in scientific notation.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16049
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that in your original line of code:

you are subtracting 1490000000000.00 off of the number, which is a double literal (because of the decimal point in the number). This means that the result of that whole calculation is double, a floating-point number, and not long, an integer.

If you need a long, then don't subtract a double value from the number; subtract a long value from the number instead:

 
Steve Dyke
Ranch Hand
Posts: 1779
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Note that in your original line of code:

you are subtracting 1490000000000.00 off of the number, which is a double literal (because of the decimal point in the number). This means that the result of that whole calculation is double, a floating-point number, and not long, an integer.

If you need a long, then don't subtract a double value from the number; subtract a long value from the number instead:



Thanks, this works great. However, I did not consider my seed number growing as fast as it has. Now the number is 1500063579542 and subtracting the 1490000000000L yields an 11 digit number.

I need a way to reduce any 13 digit number down to 10 digits. Is there a way to strip the last 3 digits off?
 
Campbell Ritchie
Marshal
Posts: 56225
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you want to lose 3 digits? What is wrong with / 1000?
 
Steve Dyke
Ranch Hand
Posts: 1779
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Why would you want to lose 3 digits? What is wrong with / 1000?


Thanks. This will work.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!