java中的科学记数法抽象
是否有一个java类抽象来表示科学数字(例如数字,10的幂模数)
,以便我可以在它们之上执行简单的操作,例如乘法或除法。
注意:寻找类似于 Fraction 这里。
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 multiplication or division on top of them.
Note: Looking for something similar to Fraction here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如 Emil 所说,BigDecimal 可以帮助您。它没有让您输入双尾数和 int 指数的构造函数。您可以扩展该类以创建新的构造函数,或者可以定义 DecimalFormat。我认为 DecimalFormat 类可以完成您想要的一些操作。它允许您如何定义数字的“外观”。您需要使用字符串定义格式,教程是
http://download.oracle.com/javase/tutorial/i18n/format /decimalFormat.html
您必须定义科学数字的格式,调用
yourDecimalFormat.parse(yourStringtoBecomeScientificNumber)
这将为您提供一个 Number,然后将其转换为 BigDecimal 以进行算术运算它。另请注意,这应该允许您强制将一定数量的有效数字或数字保留在小数点左侧。
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.
我没有得到你的例子。如果你需要不同类型的数字表示,那么你可以通过扩展抽象类 Number。另请查看 BigDecimals 。我认为这就是您正在寻找的。
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.
您可以将数字写为数字+e+指数,例如:
1e2 而不是 100。
乘法和除法应该适用于它。
You could write the number as number+e+exponent, e.g.:
1e2 instead of 100.
Multiplication and Division should work for it.