科学计数到小数 [英] Scientific notation to decimal

查看:101
本文介绍了科学计数到小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个双精度字,我正在尝试将其转换为十进制数。当我使用十进制格式实现此目标时,我得到以下信息:

I have a double and i am trying to convert it to a decimal. When i use decimalformat to achieve this i get the following:

public void roundNumber(){
    double d = 2.081641999208976E-4;
    JOptionPane.showMessageDialog(null,roundFiveDecimals(d));
}

public double roundFiveDecimals(double d) {
    DecimalFormat df = new DecimalFormat("#.#####");
    return Double.valueOf(df.format(d));
}

我希望输出为.00021;但是,我得到2.1E-4。谁能帮助解释如何获取.00021而不是2.1E-4?

I want the output to be .00021; however, i get 2.1E-4. Can anyone help explain how to get .00021 and not 2.1E-4?

推荐答案

您正在解析<来自 DecimalFormat 的结果-您应该以 String 的形式返回

You're parsing the result from DecimalFormat - you should be returning it as a String:

public String roundFiveDecimals(double d) {
    DecimalFormat df = new DecimalFormat("#.#####");
    return df.format(d);
}

double 的值本身没有格式的概念-只是一个数字。 DecimalFormat 的工作是将值格式化为文本,但是您需要...如果将文本 back 转换为数字,则已经失去了这项工作。

The double value itself has no concept of formatting - it's just a number. It's the job of DecimalFormat to format the value into text however you want... if you then convert that text back into a number, you've lost that work.

这篇关于科学计数到小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆