Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Decimal format in JAVA !!!!!!!!

  • 10-02-2004 02:54PM
    #1
    Closed Accounts Posts: 1,637 ✭✭✭


    I have 3 input dialogs taking 3 values that are then mulitpyed, is there any way of setting it so that only 2 decimal places are shown,

    e.g. 3.22 instead of

    3.22222222222222222222222222

    I'll post the code if you want but its a bit long, if you could give me some code examples that would be great.

    Thanks loads in advance..

    Thanks joePC...


Comments

  • Closed Accounts Posts: 1,637 ✭✭✭joePC


    I've found all the articals on the java site but it still doesnt make much sence to me, "Im new @ the java"

    Thanks joePC


  • Registered Users, Registered Users 2 Posts: 696 ✭✭✭Kevok


    Dirty method, sure theres probably a better way but;

    Multiply the answer by 100. Cast it into an integer to drop all the sub decimal places, then divide by 100 casting into a double or float. Then return the double/float.

    [Edit]You're new at Java sorry:

    int temp = (int)answer*100; // (int) casts the number into an integer dropping everything below the .
    return ((double)temp/100);// (double) does the same for decimal

    [/Edit]


  • Closed Accounts Posts: 1,637 ✭✭✭joePC


    Thanks for that Kevok that works perfectly..

    Thanks joePC


  • Registered Users, Registered Users 2 Posts: 1,186 ✭✭✭davej


    Funnily enough there is a class called..ahem DecimalFormat that you can use.

    java.text.DecimalFormat df=new DecimalFormat("0.00");
    float f = 10.2334343556;
    System.out.println(df.format(f));

    davej


  • Registered Users, Registered Users 2 Posts: 5,618 ✭✭✭Civilian_Target


    Does that save the result back into float f or do you have to use it in the form

    float newvar = df.format(f);

    ?


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,186 ✭✭✭davej


    it returns a string...
    (edited my previous post before i saw your question)

    davej


  • Registered Users, Registered Users 2 Posts: 19,396 ✭✭✭✭Karoma


    if it's just for the returned result, return as a String and format


  • Registered Users, Registered Users 2 Posts: 6,240 ✭✭✭hussey


    and to revert a string back to a float :
    /* assume var num is a float, and word is the string value*/
    try {
    
          num = java.lang.Float.parseFloat(word);
    
    }catch (NumberFormatException nfe) {
          system.out.println("Number format exception : make sure \"" + word + "\" is valid");
    }
    


Advertisement