Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to express numbers in scientific notation in java? [duplicate]

I'm writing a program that deals with planets' mass and diameter; These quantities are expressed in scientific notation. My question is NOT, mind you, NOT how does one print large numbers the right way (That's using printf(), duh), its how I would... "type" these numbers, I guess you could say. For example, the mass of mercury is expressed:

3.30 x 10ˆ23 

And in my array of planet masses, an element would look:

33.0 * Math.pow(10, 23) 

However, I don't quite think this is the right way - it looks like it would throw an exception... So how could I express large numbers like that from a programmer's perspective? Thanks!

like image 934
OrangeCalx01 Avatar asked Nov 14 '13 17:11

OrangeCalx01


People also ask

How do you double scientific notation?

To multiply two numbers in scientific notation, multiply their coefficients and add their exponents.

How do I print a double value without scientific notation using Java?

double dexp = 12345678; System. out. println("dexp: "+dexp);

How do you do E notation in Java?

In most programming languages, including Java, an E followed by either a + or a - is used to represent “× 10 to the power”; for example, 6.0220943E+23 or 6.25E-27 erg-seconds.


1 Answers

Section 3.10.2 of the JLS talks about floating-point literals in Java. In short, provide the decimal part as if it were scientific notation, but instead of x 10^23 you would write e23:

3.30e23 

To write one with a negative exponent, you can do that easily also for 6.67 x 10^(-11):

6.67e−11 
like image 115
rgettman Avatar answered Oct 14 '22 23:10

rgettman