1

Suppose I have money value represented as 10.000,00 in congo currency and I want it to be converted to a decimal amount like 10000.00. The money representation can be any from deferent countries and the outcome should be a decimal amount.

How do I achieve this in java?

Prasanta Biswas
  • 761
  • 2
  • 14
  • 26
  • I did that just recently like this: Split the string by all possible markers (.,' should be enough). Then join the parts except for the last one. Before you add that, you add whatever decimal-sign you need. So, "10.000,00" => [[10],[000],[00]] => 10000.00. This can then be converted to BigDecimal. (Don't use float or double for monetary values.) – Fildor Apr 10 '17 at 08:12
  • 1
    I don't know if that's interesting for you, but Java 8 has a `Currency` class, that can give you [number of fraction didgits](https://docs.oracle.com/javase/8/docs/api/java/util/Currency.html#getDefaultFractionDigits--). – Fildor Apr 10 '17 at 08:22
  • Will you know what currency or locale to expect? – Fildor Apr 10 '17 at 09:45

4 Answers4

0

It is better to keep money in integer format in minimal possible coin value - f.e. in US dollars - if you have 1$ and 25 cents - just save it as int 125 cents - it helps to avoid very common problems with java float/doubles precision errors.

  • 1
    Mind that you'll have to keep the information which currency it is, since not all currencies have 2 digits decimal value. Some have 3 decimals, others none! Also in some financial applications, you might need 4 decimals (= cent/100). – Fildor Apr 10 '17 at 08:16
  • 1
    This way he won't be able to represent beyond USD 21 Million or even worse 21 Million Indonesian Rupiah (USD 1700) – Rajeev Sreedharan Apr 10 '17 at 08:26
  • Ref https://en.wikipedia.org/wiki/ISO_4217 for the recognised resource. – MikeRoger Feb 15 '18 at 10:58
0

You can use replace for example :

money.replace(",", "#").replace(".", "").replace("#", ".")

This will gives you:

10000.00

If you want to get the decimal value then you can use :

String money = "10.000,00";
NumberFormat format = NumberFormat.getInstance(Locale.FRANCE);
Number number = format.parse(money.replace(".", ""));
System.out.println(number);
double d = number.doubleValue();
System.out.println(d);

This will gives you:

10000.0
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
0

Note: For the following, I assume it will not necessarily be known beforehand which currency/locale to expect.

I did that just recently like this: Split the string by all possible markers (.,' should be enough). Then join the parts except for the last one. Before you add that, you add whatever decimal-sign you need. So, "10.000,00" => [[10],[000],[00]] => 10000.00. This can then be converted to BigDecimal. (Don't use float or double for monetary values.)

The reason for doing it that way: You can handle most formats like

  • 1.234,56
  • 1,234.56
  • 1'234.56
  • 1234.567

And you automatically can handle numbers of fraction digits != 2.

You'd only have to be careful with whitespaces and currencies with no fractions (split array will be of size = 1). Also be careful with currencies with 3 fraction digits. It can be cumbersome to tell amounts apart using this approach that are not using a decimal at all and those that do ("1.234" =>"1234.000" and "1.234,567" => "1234.567"). You might need a little additional validation & correction for those cases.

I don't know if that's interesting for you, but Java 8 has a Currency class, that can give you number of fraction digits.

I also suggest writing a unit test, so you can be sure all expected input formats will end up in your desired format. And of course I suggest extensive documentation of what formats are accepted / allowed.

Community
  • 1
  • 1
Fildor
  • 14,510
  • 4
  • 35
  • 67
0

The method NumberFormat.getCurrencyInstance(Locale) is made exactly for this purpose.
It takes care for all country-specific formatting (decimal point or comma, currency name, currency before or after number, thousands grouping, ...).

You use it like this, for example for Congo (Democratic Republic):

Locale locale = new Locale("", "CD");
NumberFormat currencyFormat= NumberFormat.getCurrencyInstance(locale);
String s = currencyFormat.format(10000.0);

Result is "CDF 10,000.00"

Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
  • While this is a good answer, it assumes you *know* which currency is incoming. Not sure if this is the case here. – Fildor Apr 10 '17 at 09:42
  • @Fildor More precisely: you need to know the *country*. However, there is also method `getCurrentInstance()` for using the default locale, i.e. the country where you are. – Thomas Fritsch Apr 10 '17 at 09:47
  • Correct, and that might be not enough. I can be in Germany and an Amount formatted in Swiss format coming in ... Or even more weird: I can be in Germany, have a document in German language but amounts printed on it use English format ... this can get really really complicated. – Fildor Apr 10 '17 at 09:51
  • I agree with @Fildor here. – Prasanta Biswas Apr 10 '17 at 11:28