我有一个双重值= 1.068879335 我只想以1.07(例如1.07)等两个小数值将其围起来。

我尝试过这样

DecimalFormat df=new DecimalFormat("0.00");
String formate = df.format(value);
double finalValue = Double.parseDouble(formate) ;

这给了我以下例外

java.lang.NumberFormatException: For input string: "1,07"
     at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1224)
     at java.lang.Double.parseDouble(Double.java:510)

有人可以告诉我我的代码怎么了。

最后我需要finalvalue = 1.07;

有帮助吗?

解决方案

注意字符串中的逗号:“ 1,07”。 DecimalFormat 使用特定区域特定的分隔符,而 Double.parseDouble() 才不是。当您碰巧生活在小数分离器为“”的国家 /地区时,您无法解析您的电话号码。

但是,您可以使用相同的 DecimalFormat 解析它:

DecimalFormat df=new DecimalFormat("0.00");
String formate = df.format(value); 
double finalValue = (Double)df.parse(formate) ;

但是您真的应该这样做:

double finalValue = Math.round( value * 100.0 ) / 100.0;

笔记: 正如已经指出的那样,如果您不需要精确控制准确性,则应使用浮点。 (财务计算是何时 不是 使用它们。)

其他提示

实时 @Sergey的解决方案,但具有整数部门。

double value = 23.8764367843;
double rounded = (double) Math.round(value * 100) / 100;
System.out.println(value +" rounded is "+ rounded);

印刷

23.8764367843 rounded is 23.88

编辑:正如谢尔盖(Sergey)指出的那样,乘以双*int和double*double和Dible double/int和double/double之间没有区别。我找不到结果不同的示例。但是,在X86/X64和其他系统上,有一个特定的机器代码指令,用于混合双重,INT值,我相信JVM使用了。

for (int j = 0; j < 11; j++) {
    long start = System.nanoTime();
    for (double i = 1; i < 1e6; i *= 1.0000001) {
        double rounded = (double) Math.round(i * 100) / 100;
    }
    long time = System.nanoTime() - start;
    System.out.printf("double,int operations %,d%n", time);
}
for (int j = 0; j < 11; j++) {
    long start = System.nanoTime();
    for (double i = 1; i < 1e6; i *= 1.0000001) {
        double rounded = (double) Math.round(i * 100.0) / 100.0;
    }
    long time = System.nanoTime() - start;
    System.out.printf("double,double operations %,d%n", time);
}

印刷

double,int operations 613,552,212
double,int operations 661,823,569
double,int operations 659,398,960
double,int operations 659,343,506
double,int operations 653,851,816
double,int operations 645,317,212
double,int operations 647,765,219
double,int operations 655,101,137
double,int operations 657,407,715
double,int operations 654,858,858
double,int operations 648,702,279
double,double operations 1,178,561,102
double,double operations 1,187,694,386
double,double operations 1,184,338,024
double,double operations 1,178,556,353
double,double operations 1,176,622,937
double,double operations 1,169,324,313
double,double operations 1,173,162,162
double,double operations 1,169,027,348
double,double operations 1,175,080,353
double,double operations 1,182,830,988
double,double operations 1,185,028,544

问题在于,您使用一个本地化格式化器,该格式化者在您的情况下生成本地特定的小数点,即“”。但是double.parsedouble()期望非定位的双文字。您可以使用特定于区域的解析方法或将格式化器的语言环境更改为使用“”的东西来解决问题。作为小数点。甚至更好的是,通过使用这样的东西避免不必要的格式:

double rounded = (double) Math.round(value * 100.0) / 100.0;

您要做的事情从根本上有问题。二进制浮点值不 小数点。您无法将一个圆形倒入一个给定数量的小数位,因为大多数“圆形”小数值根本不能表示为二进制分数。这就是为什么永远不应该使用的原因 float 或者 double 代表钱。

因此,如果您想在结果中获得小数点,则该结果必须是 String (您已经与 DecimalFormat),或 BigDecimal (有一个 setScale() 完全可以完成您想要的方法)。否则,结果不能是您想要的。

浮点指南 了解更多信息。

尝试以下尝试:org.apache.commons.math3.util.precision.round(double x,int scale)

看:http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/util/precision.html

Apache Commons数学库主页是:http://commons.apache.org/proper/commons-math/index.html

该方法的内部插入是:

public static double round(double x, int scale) {
    return round(x, scale, BigDecimal.ROUND_HALF_UP);
}

public static double round(double x, int scale, int roundingMethod) {
    try {
        return (new BigDecimal
               (Double.toString(x))
               .setScale(scale, roundingMethod))
               .doubleValue();
    } catch (NumberFormatException ex) {
        if (Double.isInfinite(x)) {
            return x;
        } else {
            return Double.NaN;
        }
    }
}

您可以尝试定义一个新的分数,并将其用作新的双重变量的双重结果。

给出的示例使您理解我刚才所说的。

double decimalnumber = 100.2397;
DecimalFormat dnf = new DecimalFormat( "#,###,###,##0.00" );
double roundednumber = new Double(dnf.format(decimalnumber)).doubleValue();

这是不可能的,因为有两个小数位数的数字无法完全使用IEEE浮点数(例如1/10 = 0.1)表达 Double 或者 Float)。格式应始终作为最后一步发生,然后再向用户提出结果。

我想您在问,因为您想处理货币价值。没有办法使用浮点数可靠地执行此操作,您可以考虑切换到定点算术。这可能意味着在“美分”而不是“美元”中进行所有计算。

double TotalPrice=90.98989898898;

  DecimalFormat format_2Places = new DecimalFormat("0.00");

    TotalPrice = Double.valueOf(format_2Places.format(TotalPrice));

您可以使用格式 这里,

  public static double getDoubleValue(String value,int digit){
    if(value==null){
        value="0";
     }
    double i=0;
     try {
         DecimalFormat digitformat = new DecimalFormat("#.##");
         digitformat.setMaximumFractionDigits(digit);
        return Double.valueOf(digitformat.format(Double.parseDouble(value)));

    } catch (NumberFormatException numberFormatExp) {
        return i;   
    }
}

如果您不想使用DemalFormat(例如由于其效率),并且需要使用一般解决方案,则也可以尝试使用该方法的方法:

public static double roundToDigits(double value, int digitCount) {
    if (digitCount < 0)
        throw new IllegalArgumentException("Digit count must be positive for rounding!");

    double factor = Math.pow(10, digitCount);
    return (double)(Math.round(value * factor)) / factor;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top