简体   繁体   中英

Java floating point number in comma instead of dot

All I have a floating point number in Finnish local. It is like the following:-

  String numberString = "1000,30";
  if(numberString.contains(",")){
        numberString = numberString.replaceAll(",",".").trim();    
    }

    try {
        Number number = Double.parseDouble(numberString);
        return number;
    }catch (NumberFormatException ex){
        System.out.printf(ex.getMessage());
    }
    return null;

But this number has value 1000.30. I would like the number should I have value 1000,30. How can I have Number with the comma instead of the dot? This is a basic question it must have been asked earlier. But all I find is in String data type. I don't see any Number data type.

When seeing your comment that the API accepts only Number and that it calls Number#toString() on it, then I see only 1 way to enforce the rightful display. By using your own implementation of Number and overwriting the way Object#toString() works:

public class CorrectlyDisplayedDouble extends Number{
    private final Double internal;

    public CorrectlyDisplayedDouble(Double internal){
        this.internal = internal;
    }

    public int intValue(){
        return internal.intValue();
    }

    public long longValue(){
        return internal.longValue();
    }

    public float floatValue(){
        return internal.floatValue();
    }

    public double doubleValue(){
        return internal.doubleValue();
    }

    public String toString(){
        // replaces periods with commas
        return internal.toString().replace(".", ",");
    }
}

Which can then be easily created using following snippet, which then also can be passed to your third party API:

Number number = new CorrectlyDisplayedDouble(Double.parseDouble(numberString));

Sample Code :

String number = "1000500000.574";
double amount = Double.parseDouble(number);
DecimalFormat formatter = new DecimalFormat("#,###.00");
System.out.println(formatter.format(amount));

This will help you to understand how Locale exactly works:

public double parse(String decimalAsText) {
    NumberFormat decimalFormat = NumberFormat.getNumberInstance(Locale.FRANCE);
    decimalAsText = decimalAsText.replace(' ', '\u00a0');
    ParsePosition pp = new ParsePosition(0);
    Number n = decimalFormat.parse(decimalAsText, pp);
    return n.doubleValue();
}
public String parse(double textAsDecimal) {
    NumberFormat nf = NumberFormat.getNumberInstance(Locale.FRENCH);
    BigDecimal bd = BigDecimal.valueOf(textAsDecimal);
    nf.setMaximumFractionDigits(bd.scale());
    String s = nf.format(textAsDecimal);
    return s;
}

You can use DecimalFormat and Locale, for example:

DecimalFormat dfFrance = (DecimalFormat)DecimalFormat.getInstance(Locale.FRANCE);
dfFrance.applyPattern("###.00");
Number n = 0;
try {
    n = dfFrance.parse("1000,30");
} catch (ParseException e) {
    e.printStackTrace();
}
System.out.println(n.doubleValue());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM