Results 1 to 6 of 6
  1. #1
    mohammad8065 is offline Member
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

    Default number of floating point

    hi. i wrote a program that makes float numbers between 0 and 1 .number of floating point is 8 foe example 0.01234444.but i want to make this randoms with 2 flaoting points for example 0.22.but i cant.please help me.:confused:

  2. #2
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    OK, it depends exactly on what guarantee you need about the number and what use you want to put to it.

    Remember that when you create a floating point number, it is generally not completely accurate. In particular, if the fraction is not a negative power of two (1/2, 1/4, 1/8 etc) or a combination of such fractions, then the number cannot be stored completely accurately. So there is no way to guarantee that a 'float' or 'double' will be accurate to precisely two and only two decimal places. If you "ask" for 0.22 etc, you will actually get something like 0.2200000000001 etc. So depending on what you need, you have a few options:
    - if you just need to display two random digits but "pretend" that they represent a fraction between 0 and 1, then you may as well just generate two random decimal digits with rand.nextInt(10) and manually print a decimal point followed by the digits (you can also do rand.nextInt(100), remembering to adjust your display code for the case of '00');
    - if you need to choose or calculate with a random choice from the values 0.01, 0.02, 0.03 etc, but the inaccuracy of float/double doesn't bother you, then you can use:
    Java Code:
    double randDecimal = (double) rand.nextInt(100) / 100d;
    - if you need to calculate with such numbers, and the precision is completely paramount, then look at the BigDecimal class (which you can instantiate with two random digits).

    In the examples above, 'rand' represents an instance of Random, of course.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    There are two issues here, one being the significant digits of the number itself, which I think you don't want to mess with, and the other being the significant digits of the number as it is displayed, and this you probably want to control.

    So on that note, I recommend that you look into either String.format(...) or DecimalFormat to control how your floating point number is displayed.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.util.Random;
    
    public class Test {
        public static void main(String[] args) {
            Random r = new Random();
            for(int i = 0; i < 10; i++) {
                double f = r.nextFloat();
                System.out.println("f = " + f);
                System.out.printf("\tformatted = %.2f", f);
                float truncated = ((int)(f*100))/100f;
                System.out.print("  truncated = " + truncated);
                float rounded = Math.round(f*100)/100f;
                System.out.print("  rounded = " + rounded);
                System.out.println();
            }
        }
    }

  5. #5
    KingZebulon's Avatar
    KingZebulon is offline Member
    Join Date
    Dec 2008
    Posts
    5
    Rep Power
    0

    Default

    I had this problem in the past, and the DecimalFormat class worked the best for me.

    Java Code:
    import java.text.DecimalFormat;
    
    float floatNum;
    DecimalFormat df = new DecimalFormat("#,###.00");
    
    System.out.println(df.format(floatNum));
    The "#,###.00" shows the number of places to show after the decimal. You can add or subtract zeros and number signs to change your look. Of course, this is not exact, but you can plug these lines into your code and it should work fine :)

    Cheers

  6. #6
    mohammad8065 is offline Member
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

    Default

    Thanks all of you very much.i have a project with GA and you solved my problem.Thank you very much again.

Similar Threads

  1. Floating point values in SWT Spinner
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-07-2008, 04:50 PM
  2. is it bug? or am i missing a point?
    By blowguy in forum Threads and Synchronization
    Replies: 1
    Last Post: 11-20-2007, 06:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •