DecimalFormat and Double.valueOf()

后端 未结 10 1176
鱼传尺愫
鱼传尺愫 2020-12-24 15:22

I\'m trying to get rid of unnecessary symbols after decimal seperator of my double value. I\'m doing it this way:

DecimalFormat format = new DecimalFormat(\"         


        
相关标签:
10条回答
  • 2020-12-24 16:04

    For the ',' instead of the '.' , you'd have to change the locale.

    For the number of decimals, use setMaximumFractionDigits(int newValue).

    For the rest, see the javadoc.

    0 讨论(0)
  • 2020-12-24 16:06

    By

    get rid of unnecessary symbols after decimal seperator of my double value

    do you actually mean you want to round to e.g. the 5th decimal? Then just use

    value = Math.round(value*1e5)/1e5;
    

    (of course you can also Math.floor(value*1e5)/1e5 if you really want the other digits cut off)

    0 讨论(0)
  • 2020-12-24 16:07

    My code function :

     private static double arrondi(double number){
             DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance();
             symbols.setDecimalSeparator('.');
             DecimalFormat format = new DecimalFormat("#.#####", symbols);
             return Double.valueOf(format.format(number));
         }
    
    0 讨论(0)
  • 2020-12-24 16:10

    Use Locale.getDefault() to get your system's decimal separator which you can also set. You can't have two different separators at the same time since the other is then usually used as the separator for thousands: 2.001.000,23 <=> 2,001,000.23

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