Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Java and Currency Counting

Posted on 2011-02-18
13
343 Views
Last Modified: 2012-05-11
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!!
0
Comment
Question by:Greengiants15
  • 9
  • 3
13 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 34931077
You just use replace before you use big decimal
String s1 = s.replace(",",".")
is this tyour question?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34931099
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
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34931103
If you want then to use them as numbers
you can after that use

double x =  Double.parseDouble(s1);
0
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
LVL 47

Expert Comment

by:for_yan
ID: 34931119
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




0
 
LVL 92

Assisted Solution

by:objects
objects earned 250 total points
ID: 34931131
0
 

Author Comment

by:Greengiants15
ID: 34931174
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?
0
 
LVL 92

Expert Comment

by:objects
ID: 34931193
NumberFormat should handle it for you
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34931195
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).
0
 
LVL 92

Expert Comment

by:objects
ID: 34931217
actually your values aren't actually currency values, they are just numbers.
you should just need to specify locale to use for parsing
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34931237
@objects:
Still, this throws exception for me in Java 1.5:
 
NumberFormat format = NumberFormat.getCurrencyInstance(Locale.FRANCE);
         Object value = format.parse("5,45");
       
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34931244
@objects:
Yes without curreny it works:

NumberFormat format = NumberFormat.getInstance(Locale.FRANCE);
         Object value = format.parse("5,45");
       
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34931254
But this returns just "1", rather than "1115.45"
 
  NumberFormat format = NumberFormat.getInstance(Locale.FRANCE);
         Object value = format.parse("1.115,45");
       
        System.out.println("value: " + value);
0
 
LVL 47

Accepted Solution

by:
for_yan earned 250 total points
ID: 34931280

This works for both 5,45 and 1.115,45:

    NumberFormat format = NumberFormat.getInstance(Locale.GERMANY);
         Object value = format.parse("5,45");
       
        System.out.println("value: " + value);
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
windows explorer path to command prompt 5 54
servlet example 11 49
Java basic valueOf question 1 29
Fast way to search item into Java Array (Rhino compatible) 2 31
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question