Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to parse a currency Amount (US or EU) to float value in Java
    primarykey
    data
    text
    <p>In Europe decimals are separated with '<strong>,</strong>' and we use optional '<strong>.</strong>' to separate thousands. I allow currency values with:</p> <ul> <li>US-style 123,456.78 notation </li> <li>European-style 123.456,78 notation </li> </ul> <p>I use the next regular expression (from RegexBuddy library) to validate the input. I allow optional two-digits fractions and optional thousands separators.</p> <pre><code>^[+-]?[0-9]{1,3}(?:[0-9]*(?:[.,][0-9]{0,2})?|(?:,[0-9]{3})*(?:\.[0-9]{0,2})?|(?:\.[0-9]{3})*(?:,[0-9]{0,2})?)$ </code></pre> <p>I would like to parse a currency string to a float. For example</p> <p>123,456.78 should be stored as 123456.78<br> 123.456,78 should be stored as 123456.78<br> 123.45 should be stored as 123.45<br> 1.234 should be stored as 1234 12.34 should be stored as 12.34</p> <p>and so on...</p> <p>Is there an easy way to do this in Java?</p> <pre><code>public float currencyToFloat(String currency) { // transform and return as float } </code></pre> <p><strong>Use BigDecimal instead of Float</strong></p> <hr> <p>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. </p> <p><strong>Solution</strong></p> <hr> <p>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. </p> <pre><code> 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()&gt;=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; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload