16

In Europe decimals are separated with ',' and we use optional '.' to separate thousands. I allow currency values with:

  • US-style 123,456.78 notation
  • European-style 123.456,78 notation

I use the next regular expression (from RegexBuddy library) to validate the input. I allow optional two-digits fractions and optional thousands separators.

^[+-]?[0-9]{1,3}(?:[0-9]*(?:[.,][0-9]{0,2})?|(?:,[0-9]{3})*(?:\.[0-9]{0,2})?|(?:\.[0-9]{3})*(?:,[0-9]{0,2})?)$

I would like to parse a currency string to a float. For example

123,456.78 should be stored as 123456.78
123.456,78 should be stored as 123456.78
123.45 should be stored as 123.45
1.234 should be stored as 1234 12.34 should be stored as 12.34

and so on...

Is there an easy way to do this in Java?

public float currencyToFloat(String currency) {
    // transform and return as float
}

Use BigDecimal instead of Float


Thanks to everyone for the great answers. I have changed my code to use BigDecimal instead of float. I will keep previous part of this question with float to prevent people from doing the same mistakes I was gonna do.

Solution


The next code shows a function which transforms from US and EU currency to a string accepted by BigDecimal(String) constructor. That it is to say a string with no thousand separator and a point for fractions.

   import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class TestUSAndEUCurrency {

    public static void main(String[] args) throws Exception {       
        test("123,456.78","123456.78");
        test("123.456,78","123456.78");
        test("123.45","123.45");
        test("1.234","1234");
        test("12","12");
        test("12.1","12.1");
        test("1.13","1.13");
        test("1.1","1.1");
        test("1,2","1.2");
        test("1","1");              
    }

    public static void test(String value, String expected_output) throws Exception {
        String output = currencyToBigDecimalFormat(value);
        if(!output.equals(expected_output)) {
            System.out.println("ERROR expected: " + expected_output + " output " + output);
        }
    }

    public static String currencyToBigDecimalFormat(String currency) throws Exception {

        if(!doesMatch(currency,"^[+-]?[0-9]{1,3}(?:[0-9]*(?:[.,][0-9]{0,2})?|(?:,[0-9]{3})*(?:\\.[0-9]{0,2})?|(?:\\.[0-9]{3})*(?:,[0-9]{0,2})?)$"))
                throw new Exception("Currency in wrong format " + currency);

        // Replace all dots with commas
        currency = currency.replaceAll("\\.", ",");

        // If fractions exist, the separator must be a .
        if(currency.length()>=3) {
            char[] chars = currency.toCharArray();
            if(chars[chars.length-2] == ',') {
                chars[chars.length-2] = '.';
            } else if(chars[chars.length-3] == ',') {
                chars[chars.length-3] = '.';
            }
            currency = new String(chars);
        }

        // Remove all commas        
        return currency.replaceAll(",", "");                
    }

    public static boolean doesMatch(String s, String pattern) {
        try {
            Pattern patt = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
            Matcher matcher = patt.matcher(s);
            return matcher.matches();
        } catch (RuntimeException e) {
            return false;
        }           
    }  

}
Sergio del Amo
  • 76,835
  • 68
  • 152
  • 179
  • 1
    So what does 1.23 convert to? Your ruleset is contradictory. Why not take advantage of the localisation facilities at the client end of you application? – spender Jun 08 '09 at 16:47
  • OK, my bad... but really, trying to decipher this without knowing the locale from which it came has a bad smell. – spender Jun 08 '09 at 16:50
  • Well, basically I am mostly interested in solving the problem for EU notation style. But it would be nice to come with a standard solution. – Sergio del Amo Jun 08 '09 at 16:53
  • @sergio: updated my answer. try the existing locale-specific NumberFormats, or create a custom one with your expected formatting. – Michael Petrotta Jun 08 '09 at 18:12

4 Answers4

38

To answer a slightly different question: don't use the float type to represent currency values. It will bite you. Use a base-10 type instead, like BigDecimal, or an integer type like int or long (representing the quantum of your value - penny, for example, in US currency).

You will not be able to store an exact value - 123.45, say, as a float, and mathematical operations on that value (such as multiplication by a tax percentage) will produce rounding errors.

Example from that page:

float a = 8250325.12f;
float b = 4321456.31f;
float c = a + b;
System.out.println(NumberFormat.getCurrencyInstance().format(c));
// prints $12,571,782.00 (wrong)

BigDecimal a1 = new BigDecimal("8250325.12");
BigDecimal b1 = new BigDecimal("4321456.31");
BigDecimal c1 = a1.add(b1);
System.out.println(NumberFormat.getCurrencyInstance().format(c1));
// prints $12,571,781.43 (right)

You don't want to muck with errors when it comes to money.

With respect to the original question, I haven't touched Java in a little while, but I know that I'd like to stay away from regex to do this kind of work. I see this recommended; it may help you. Not tested; caveat developer.

try {
    String string = NumberFormat.getCurrencyInstance(Locale.GERMANY)
                                            .format(123.45);
    Number number = NumberFormat.getCurrencyInstance(locale)
                                            .parse("$123.45");
    // 123.45
    if (number instanceof Long) {
       // Long value
    } else {
       // too large for long - may want to handle as error
    }
} catch (ParseException e) {
// handle
}

Look for a locale with rules that match what you expect to see. If you can't find one, use multiple sequentially, or create your own custom NumberFormat.

I'd also consider forcing users to enter values in a single, canonical format. 123.45 and 123.456 look way too similar for my tastes, and by your rules would result in values that differ by a factor of 1000. This is how millions are lost.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
  • Let me see, if I understand you. You mean I should use int and store in my database the value as cents. 12.35 as 12 * 100 + 35 = 1235. – Sergio del Amo Jun 08 '09 at 16:52
  • It sounds clever but I would need nonetheless a function to convert from a currency String to cents – Sergio del Amo Jun 08 '09 at 16:52
  • You would, yes. What I wrote above doesn't address that. I saw you suggesting using float to handle money, and alarm bells went off. – Michael Petrotta Jun 08 '09 at 16:54
  • I suggest a decimal type over an integer type, anyway. Many databases have a native decimal type. – Michael Petrotta Jun 08 '09 at 16:54
  • 2
    seriously listen to this guy, using floats for money is going to hurt you at some point. – Gareth Davis Jun 08 '09 at 16:58
  • 7
    Yeah, this is correct. For further discussion, and examples of how things would go wrong, see Effective Java (2nd ed), Item 48, "Avoid float and double if exact answers are required". Quoting some essential points: "The float and double types are particularly ill-suited for monetary calculations [...]", "The right way to solve this problem is to use BigDecimal, int or long for monetary calculations." If I recall right, the Java Puzzlers book also deals with this in one of the puzzles. – Jonik Jun 08 '09 at 17:01
  • Also on SO there's more about this; see 'Representing Monetary Values in Java', http://stackoverflow.com/questions/285680/representing-monetary-values-in-java – Jonik Jun 08 '09 at 17:08
  • @Jonik: that's one of my top-10 books, Effective Java. I learned a lot from that. – Michael Petrotta Jun 08 '09 at 17:18
  • -1 The comment on your else statement in your parse logic is wrong. If the number is not a long it is a double. In the above sample it will be a double, and it is *not* an error and it is *not* because it is too big for long. It is a double because it has a decimal value. Long is an integer. See http://ideone.com/4BCUk – Muhd Mar 15 '11 at 23:23
1

As a generalized solution you can try

char[] chars = currency.toCharArray();
chars[currency.lastIndexOf(',')] = '.';
currency = new String(chars);

instead of

if(currency.length()>=3) {
    char[] chars = currency.toCharArray();
    if(chars[chars.length-2] == ',') {
        chars[chars.length-2] = '.';
    } else if(chars[chars.length-3] == ',') {
        chars[chars.length-3] = '.';
    }
    currency = new String(chars);
}

so that fractional part can be of any length.

eatSleepCode
  • 4,427
  • 7
  • 44
  • 93
0

Try this.............

Locale slLocale = new Locale("de","DE");
        NumberFormat nf5 = NumberFormat.getInstance(slLocale);
        if(nf5 instanceof DecimalFormat) {
            DecimalFormat df5 = (DecimalFormat)nf5;
            try {
            DecimalFormatSymbols decimalFormatSymbols = DecimalFormatSymbols.getInstance(slLocale);
            decimalFormatSymbols.setGroupingSeparator('.');
            decimalFormatSymbols.setDecimalSeparator(',');
            df5.setDecimalFormatSymbols(decimalFormatSymbols);
            df5.setParseBigDecimal(true);
            ParsePosition pPosition = new ParsePosition(0);
            BigDecimal n = (BigDecimal)df5.parseObject("3.321.234,56", pPosition);
            System.out.println(n);
            }catch(Exception exp) {
                exp.printStackTrace();
            }
        }
mgooty
  • 1
-1

A quick a dirty hack could be:

String input = input.replaceAll("\.,",""); // remove *any* , or .
long amount = Long.parseLong(input);

BigDecimal bd = BigDecimal.valueOf(amount).movePointLeft(2);

//then you could use:
bd.floatValue();
//but I would seriously recommended that you don't use floats for monetary amounts.

Note this will only work if the input is in the form ###.00, ie with exactly 2 decimal places. For example input == "10,022" will break this rather naive code.

Alternative is to use the BigDecimal(String) constructor, but you'll need to convert those euro style numbers to use '.' as the decimal separator, in addition to removing the thousand separators for both.

Gareth Davis
  • 27,701
  • 12
  • 73
  • 106
  • >Aternative is to use the BigDecimal(String) constructor, but you'll need to convert those euro style numbers to use '.' as the decimal separator, in addition to removing the thousand separators for both. I could do a regular expression to replace , plus two last digits with . plus last two digits I could then replace all dots and commas with nothing but not the . comming before two last digits. I should work but it seems error prone. – Sergio del Amo Jun 08 '09 at 17:45