Android 如何避免使用科学记数法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20813631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Avoid Scientific Notation in Double?
提问by user3051834
Here is my simple code
这是我的简单代码
@Override
public void onClick(View v) {
try {
double price = Double.parseDouble(ePrice.getText().toString());
double percent = Double.parseDouble(ePercent.getText().toString());
double priceValue = price * percent/100.0f;
double percentValue = price - priceValue;
moneyToGet.setText(String.valueOf(priceValue));
moneyToPay.setText(String.valueOf(percentValue));
moneyToGet.setText("" + priceValue);
moneyToPay.setText("" + percentValue);
// catch
} catch (NumberFormatException ex) {
// write a message to users
moneyToGet.setText("");
}
}
});
This is a simple code for Percentage Calculator.
这是百分比计算器的简单代码。
What I want is to avoid the Scientific Notationin my Calculator cause I don't want to explain to user what is Scientific Notation.
我想要的是避免在我的计算器中使用科学记数法,因为我不想向用户解释什么是科学记数法。
For exampleif I want to calculate 100,000,000and cut 50%of it, it Should give me 50,000,000which is giving me 5.0E7And in my case this doesn't make any sense to the user. And of course I know both results are correct. Thanks in Advance.
例如,如果我想计算100,000,000并削减50%,它应该给我50,000,000,这给我5.0E7而在我的情况下,这对用户没有任何意义。当然,我知道这两个结果都是正确的。提前致谢。
回答by Josef Ra?ka
回答by Kirk
You can try this DecimalFormat
你可以试试这个 DecimalFormat
DecimalFormat decimalFormatter = new DecimalFormat("############");
number.setText(decimalFormatter.format(Double.parseDouble(result)));
回答by GareginSargsyan
I would suggest using BigDecimal
s instead of double
s. That way you will have a more precise control over your calculation precision. Also you can get a non-scientific String
using BigDecimal.toPlainString()
.
我建议使用BigDecimal
s 而不是double
s。这样您就可以更精确地控制计算精度。您也可以String
使用BigDecimal.toPlainString()
.
回答by Chirag Joshi
DecimalFormat decimalFormatter = new DecimalFormat("##.############");
decimalFormatter.setMinimumFractionDigits(2);
decimalFormatter.setMaximumFractionDigits(15);
This option will help you ##.## suffix 0 before decimal, otherwise output will be .000
此选项将帮助您##.## 在十进制前添加后缀 0,否则输出将是 .000
btc.setText(decimalFormatter.format(btcval));
use this for displaying content
使用它来显示内容
回答by insomniac
Use NumberFormater like
使用 NumberFormater 之类的
NumberFormat myformatter = new DecimalFormat("########");
String result = myformatter.format(yourValue);