scientific notation abstraction in java
Is there a java class abstraction to denote scientific numbers (e.g. the number, 10 power modulus)
, so that I can do simple operations like multiplic开发者_如何转开发ation or division on top of them.
Note: Looking for something similar to Fraction here.
As Emil said, BigDecimal helps you. It doesn't have a constructor that lets you enter a double mantissa and an int exponent. You could extend the class to make a new constructor, or, you could define a DecimalFormat. I think that the class DecimalFormat will do some of what you want. It allows you how to define how a number 'looks'. You need to define the format using a String, the tutorial is http://download.oracle.com/javase/tutorial/i18n/format/decimalFormat.html
You would have to define the format of scientific numbers, call yourDecimalFormat.parse(yourStringtoBecomeScientificNumber)
which will give you a Number, then cast it as a BigDecimal to do arithmetic on it.
Also note that this should allow you to force a certain number of significant figures or digits to left of decimal point.
I didn't get your example.If you need a different type of number representation then you can do so by extending abstract class Number.Also have a look at BigDecimals .I think this is what you are looking for.
You could write the number as number+e+exponent, e.g.: 1e2 instead of 100. Multiplication and Division should work for it.
精彩评论