Greengiants15
asked on
Java and Currency Counting
Hi Experts,
I want to count a set of numbers that happen to be in EUR format (not USD).
3,45 translates to 3.45 in US
I want to count a bunch of numbers that are in EUR format. Can anyone offer any tips on how to do this?
Ex:
345,99
200,99
567,83
Total = 1.114,81
I know that BigDecimal will blow up if I use commas so I'm not sure what else to use.
Thank you!!
I want to count a set of numbers that happen to be in EUR format (not USD).
3,45 translates to 3.45 in US
I want to count a bunch of numbers that are in EUR format. Can anyone offer any tips on how to do this?
Ex:
345,99
200,99
567,83
Total = 1.114,81
I know that BigDecimal will blow up if I use commas so I'm not sure what else to use.
Thank you!!
Or maybe even better:
String s = "1.114,81";
String s1 = "";
for(int j=0; j<s.length(); j++){
if(s.charAt(j)== '.')continue;
if(s.charAt(j)==',')s1 = s1 + ".";
else s1 = s1 + s.charAt(j);
}
System.out.println(s1);
it should print:
1114.81
String s = "1.114,81";
String s1 = "";
for(int j=0; j<s.length(); j++){
if(s.charAt(j)== '.')continue;
if(s.charAt(j)==',')s1 = s1 + ".";
else s1 = s1 + s.charAt(j);
}
System.out.println(s1);
it should print:
1114.81
If you want then to use them as numbers
you can after that use
double x = Double.parseDouble(s1);
you can after that use
double x = Double.parseDouble(s1);
You can of course make a method out of it:
String convert(String s){
String s1 = "";
for(int j=0; j<s.length(); j++){
if(s.charAt(j)== '.')continue;
if(s.charAt(j)==',')s1 = s1 + ".";
else s1 = s1 + s.charAt(j);
}
return s1;
}
then you just sum them like
double sum;
sum += Double.parseDouble(convert (s)); - add for each number
String convert(String s){
String s1 = "";
for(int j=0; j<s.length(); j++){
if(s.charAt(j)== '.')continue;
if(s.charAt(j)==',')s1 = s1 + ".";
else s1 = s1 + s.charAt(j);
}
return s1;
}
then you just sum them like
double sum;
sum += Double.parseDouble(convert
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks objects and for Yan - once I know the currency, then are you saying I should so a String.replace() on the commas? It there not an easier way to calculate this?
NumberFormat should handle it for you
I'm not sure it all depends on currency - it rather depends on the country - on Locale - in some
countries they use commas, in some they do not use.
If your numbers come from different countries, then, i guess you should use
the way onjects suggested bu specify locale. If all your numbers are
uniform in sense of the country like that 1.111,18 then you can
use method which I wrote (replace is not enough - it will not remove the dot).
countries they use commas, in some they do not use.
If your numbers come from different countries, then, i guess you should use
the way onjects suggested bu specify locale. If all your numbers are
uniform in sense of the country like that 1.111,18 then you can
use method which I wrote (replace is not enough - it will not remove the dot).
actually your values aren't actually currency values, they are just numbers.
you should just need to specify locale to use for parsing
you should just need to specify locale to use for parsing
@objects:
Still, this throws exception for me in Java 1.5:
NumberFormat format = NumberFormat.getCurrencyIn stance(Loc ale.FRANCE );
Object value = format.parse("5,45");
Still, this throws exception for me in Java 1.5:
NumberFormat format = NumberFormat.getCurrencyIn
Object value = format.parse("5,45");
@objects:
Yes without curreny it works:
NumberFormat format = NumberFormat.getInstance(L ocale.FRAN CE);
Object value = format.parse("5,45");
Yes without curreny it works:
NumberFormat format = NumberFormat.getInstance(L
Object value = format.parse("5,45");
But this returns just "1", rather than "1115.45"
NumberFormat format = NumberFormat.getInstance(L ocale.FRAN CE);
Object value = format.parse("1.115,45");
System.out.println("value: " + value);
NumberFormat format = NumberFormat.getInstance(L
Object value = format.parse("1.115,45");
System.out.println("value:
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
String s1 = s.replace(",",".")
is this tyour question?