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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

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

  • 10-02-2004 2: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