Double value to round up in Java

后端 未结 10 1903
我寻月下人不归
我寻月下人不归 2020-12-07 12:44

I have a double value = 1.068879335 i want to round it up with only two decimal values like 1.07.

I tried like this

DecimalFormat df=new         


        
相关标签:
10条回答
  • 2020-12-07 13:11

    If you do not want to use DecimalFormat (e.g. due to its efficiency) and you want a general solution, you could also try this method that uses scaled rounding:

    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;
    }
    
    0 讨论(0)
  • 2020-12-07 13:19
    double TotalPrice=90.98989898898;
    
      DecimalFormat format_2Places = new DecimalFormat("0.00");
    
        TotalPrice = Double.valueOf(format_2Places.format(TotalPrice));
    
    0 讨论(0)
  • 2020-12-07 13:20

    The problem is that you use a localizing formatter that generates locale-specific decimal point, which is "," in your case. But Double.parseDouble() expects non-localized double literal. You could solve your problem by using a locale-specific parsing method or by changing locale of your formatter to something that uses "." as the decimal point. Or even better, avoid unnecessary formatting by using something like this:

    double rounded = (double) Math.round(value * 100.0) / 100.0;
    
    0 讨论(0)
  • 2020-12-07 13:20

    You can use format like here,

      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;   
        }
    }
    
    0 讨论(0)
  • 2020-12-07 13:25

    There is something fundamentally wrong with what you're trying to do. Binary floating-points values do not have decimal places. You cannot meaningfully round one to a given number of decimal places, because most "round" decimal values simply cannot be represented as a binary fraction. Which is why one should never use float or double to represent money.

    So if you want decimal places in your result, that result must either be a String (which you already got with the DecimalFormat), or a BigDecimal (which has a setScale() method that does exactly what you want). Otherwise, the result cannot be what you want it to be.

    Read The Floating-Point Guide for more information.

    0 讨论(0)
  • 2020-12-07 13:25

    This is not possible in the requested way because there are numbers with two decimal places which can not be expressed exactly using IEEE floating point numbers (for example 1/10 = 0.1 can not be expressed as a Double or Float). The formatting should always happen as the last step before presenting the result to the user.

    I guess you are asking because you want to deal with monetary values. There is no way to do this reliably with floating-point numbers, you shoud consider switching to fixed-point arithmetics. This probably means doing all calculations in "cents" instead of "dollars".

    0 讨论(0)
提交回复
热议问题