Results 1 to 4 of 4

Thread: Convert float to 2 decimal place

  1. #1
    Join Date
    Nov 2009
    Posts
    583

    Convert float to 2 decimal place

    Hi to everyone
    I am new to java and I have a simple question for you guys. Is there any other way to convert a float to two decimal places? the way I know, has been posted below. I tried to search on the internet but do not find any easy solution for it all the examples are too complicated.
    Code:
    f = f/99;
    This is all that I have to do to complete this task. Any advice or suggestions, please

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Convert float to 2 decimal place

    Hi
    If you are sure of that your number can fit inside an int or long you can follow this
    Code:
    long temp = 100.0 * x;
    x = temp/100.0;
    This can be done also in one line
    Code:
    x= ((long)(100.0*x))/100.0;
    My advice to you is that you have all the decimals in places and then you can display out of tow decimals precision. You can do this by
    Code:
    System.out.printf("x is %x, which is %.2x with 2 decimals precision",f,f);
    Just select which you need.

  3. #3
    Join Date
    Nov 2009
    Posts
    583

    Re: Convert float to 2 decimal place

    Hi
    Thanks for your reply. My actual code is something like this
    Code:
    double mvalue = 0;
    mvalue = mvalue + rndNumber;
    double m = mvalue / 99;
    System.out.println("The m Value is :" + m);
    The result of this program isn 1.555... The question I was asking is that, is there any way to reduce to 2 decimal points? Sorry but the one above did not really help. Any advice. Thanks in advance.

  4. #4
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Convert float to 2 decimal place

    Hi
    Float and double will never be only in two decimals, it all depends on your operating system. But you can print the float and double values wiht tow decimals. Check out this statement.
    Code:
    System.out.printf("The Mean Value is : %.2f", m);
    You can study the formatter provided by java se6 for more information.

Similar Threads

  1. Replies: 3
    Last Post: 27-11-2010, 12:59 AM
  2. c program to convert decimal to binary
    By Oswaldo in forum Software Development
    Replies: 3
    Last Post: 25-11-2009, 03:45 PM
  3. How to convert string to float c#
    By Pratyush in forum Software Development
    Replies: 3
    Last Post: 14-08-2009, 01:09 PM
  4. Convert binary to decimal in java
    By Seraphim in forum Software Development
    Replies: 2
    Last Post: 20-05-2009, 09:19 AM
  5. Convert a string in decimal
    By FlayoFish in forum Software Development
    Replies: 3
    Last Post: 23-04-2009, 12:18 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,726,492.21909 seconds with 16 queries