Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Converting Strings in Java

Options
  • 19-04-2005 12:18pm
    #1
    Registered Users Posts: 78 ✭✭


    Hi, im trying to convert a Java string to a number but i need the number to be displayed as a unit of currency i.e. 3.80. My predicament is that when i convert the string to an int or double i get 3.8 and not 3.80. Does anybody know of a method that allows me to converrt a string to a unit of currency?


Comments

  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    al. wrote:
    but i need the number to be displayed

    Hint: everything non-graphical which is displayed is a String.

    Numbers are converted to strings to display them. If you don't do it explicitly, Java will do it internally.

    So...either reconsider why you're converting from a string in the first place, or (if its still needed), convert back to a string.
    as a unit of currency i.e. 3.80.
    Without being smart, thats not a unit of currency.

    For a start, there's no currency sign. Secondly, there is no strict requirement that currency must have 2 (and only 2) decimal places. IIRC, Irish banks often calculate to 6 and display to 4 decimal places.

    But I understand what you mean...youwant your numeric values output in a formatted manner.
    My predicament is that when i convert the string to an int or double i get 3.8 and not 3.80. Does anybody know of a method that allows me to converrt a string to a unit of currency?
    What you want is something that lets you define a format for the string the number is converted into.

    Have a look at http://java.sun.com/docs/books/tutorial/i18n/format/decimalFormat.html

    jc


  • Registered Users Posts: 304 ✭✭PhantomBeaker


    I've had to do something similar... actually it was for hex values of RGB colours (I'm sure there's a better term for that I just can't think of it)

    What I'd suggest is develop a test to the length of the substring after the decimal point. If that length is greater than 2, then you have too much precision and you can round it.

    If it's less than 2, or you can't find a decimal point: append the decimal point if you need to, and then append the extra 0 characters onto the end.

    Might that be a way to think of it?

    Take care,
    Phantom Beaker.


  • Registered Users Posts: 78 ✭✭al.


    Thanks for the help, im still having a bit of trouble working it out but ill put a bit more research into it and get back to you.

    Cheers,

    Al.


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    There is probably a way already for it. But quick and easy way.

    1. Check for "." in the string.
    2. If its there add "00" to the end of the string, otherwise add ".00" to the string.
    3. Take left side of the string up to location of "." + 2.

    examples:

    34 -> 34.00 -> 34.00
    27.5 -> 27.500 -> 27.5
    3.14159 -> 3.1415900 -> 3.14


  • Closed Accounts Posts: 25 dan_pretty_boy


    Hi,

    As hobbles say there is an easier way.. -> DecimalFormat


    String t = "3.90";

    DecimalFormat df = new DecimalFormat("###.00");


    double s= Double.parseDouble(t);
    String ss = df.format(s);

    System.out.println(ss);


    Regards
    Danny


  • Advertisement
  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    As hobbles say there is an easier way.. -> DecimalFormat
    Which would be what the URL I posted linked to in the Sun documentation...

    jc


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    bonkey wrote:
    Which would be what the URL I posted linked to in the Sun documentation...

    jc

    yea but who reads links these days.. next you will be asking people to use google.


  • Registered Users Posts: 78 ✭✭al.


    Do you need to import anything before using DecimalFormat. When i try to compile my program i am getting a cannot resolve symbol error for it


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    You probably need to import the DecimalFormat class.

    Without looking at it, i'd guess it's java.util.DecimalFormat


  • Registered Users Posts: 1,745 ✭✭✭swiss




  • Advertisement
  • Registered Users Posts: 78 ✭✭al.


    Thanks for the help, i've managed to convert my Strings to doubles and then format the doubles to display a currency ie $3.80. I have something like this:

    String beer_1 = drink_list[1].price;
    DecimalFormat df1 = new DecimalFormat("###.00");
    double b1 = (Double.valueOf(drink_list[1].price).doubleValue() / 100);

    String bb1 = df1.format(b1);
    beer_text_1.setText("$ " + bb1);

    What i am doing here is taking a string from an array called drink_list and displaying it in a GUI.

    The problem i am having is that i have an array with 36 strings and i want to display each segment of the array as a formatted number in my GUI. Do i have to repeat this code 36 times to do this or is there a for loop i can use?


  • Registered Users Posts: 1,994 ✭✭✭lynchie


    If you are not aware of the basic concept of a for loop in Java, I suggest you start at the beginning again, and possibly read up on the basic java language tutorial on Sun's website.


  • Registered Users Posts: 78 ✭✭al.


    Thanks for the help, anyone else got a better idea?


  • Closed Accounts Posts: 25 dan_pretty_boy


    try

    google java for loops


    danny


  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    for(int i=0; i<whatever; i++){
    // Stuff
    }
    

    Really, tho, you should consider reading a basic Java book or having a look at the Java Tutorial on Sun's website.


  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    And as a side note, if this is for a real product, it's worth bearing in mind that many currencies use different decimal accuracy.


Advertisement