Dayanne
Dayanne

Reputation: 1435

Convert String with Dot or Comma to Float Number

I always like input in my function to get numbers that range from 0.1 to 999.9 (the decimal part is always separated by '.', if there is no decimal then there is no '.' for example 9 or 7 .

How do I convert this String to float value regardless of localization (some countries use ',' to separate decimal part of number. I always get it with the '.')? Does this depend on local computer settings?

Upvotes: 35

Views: 106531

Answers (8)

Henry Lang
Henry Lang

Reputation: 1

To convert the string to float regardless of localization you can always replace the comma "," with the dot "." using the str.replace() method, as following:

// Set value as string first
String s = "9,9";
// Replacing , with .
s = s.replace(",", ".");
// Parsing the value
float x = Float.parseFloat(s);

Upvotes: 0

robni
robni

Reputation: 1066

You can use the placeholder %s-String for any primitive type.

float x = 3.15f, y = 1.2345f;

System.out.printf("%.4s and %.5s", x, y);

Output: 3.15 and 1.234

%s is always english formatting regardless of localization.

If you want a specif local formatting, you could also do:

import java.util.Locale;

float x = 3.15f, y = 1.2345f;

System.out.printf(Locale.GERMAN, "%.2f and %.4f", x, y);

Output: 3,15 and 1,2345

Upvotes: 1

Aliton Oliveira
Aliton Oliveira

Reputation: 1339

I hope this piece of code may help you.

public static Float getDigit(String quote){
        char decimalSeparator = new DecimalFormatSymbols().getDecimalSeparator();
        String regex = "[^0-9" + decimalSeparator + "]";
        String valueOnlyDigit = quote.replaceAll(regex, "");

        if (String.valueOf(decimalSeparator).equals(",")) {
            valueOnlyDigit = valueOnlyDigit.replace(",", ".");
            //Log.i("debinf purcadap", "substituted comma by dot");
        }

        try {
            return Float.parseFloat(valueOnlyDigit);
        } catch (ArithmeticException | NumberFormatException e) {
            //Log.i("debinf purcadap", "Error in getMoneyAsDecimal", e);
            return null;
        }
    }

Upvotes: 0

cfx70
cfx70

Reputation: 63

What about this:

Float floatFromStringOrZero(String s){
    Float val = Float.valueOf(0);
    try{
        val = Float.valueOf(s);
    } catch(NumberFormatException ex){
        DecimalFormat df = new DecimalFormat();
        Number n = null;
        try{
            n = df.parse(s);
        } catch(ParseException ex2){
        }
        if(n != null)
            val = n.floatValue();
    }
    return val;
}

Upvotes: 6

ewan.chalmers
ewan.chalmers

Reputation: 16235

The Float.parseFloat() method is not locale-dependent. It expects a dot as decimal separator. If the decimal separator in your input is always dot, you can use this safely.

The NumberFormat class provides locale-aware parse and format should you need to adapt for different locales.

Upvotes: 32

flamming_python
flamming_python

Reputation: 716

valueStr = valueStr.replace(',', '.');
return new Float(valueStr);

Done

Upvotes: 25

user unknown
user unknown

Reputation: 36229

See java.text.NumberFormat and DecimalFormat:

 NumberFormat nf = new DecimalFormat ("990.0");
 double d = nf.parse (text);

Upvotes: 6

Maurice Perry
Maurice Perry

Reputation: 32831

DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator('.');
DecimalFormat format = new DecimalFormat("0.#");
format.setDecimalFormatSymbols(symbols);
float f = format.parse(str).floatValue();

Upvotes: 29

Related Questions