Round up float number

Hi,
I have to manage floats with euro format .
here's the code :
<CODE>
float first = 78.31;
float second = 1.81;
float tot = first + second;
</CODE>
the result of thi addition is tot = 80.11999......
sigh!!
how can i round up this number with 2 digits of precision ?
(tot = 80.12)
thx

You don't. But you can format it rounded. (i.e., the stuff in the java.text package, like DecimalFormat).
Or you can represent the value using ints. (E.g., create a Euro class and have two ints, one for the whole euro value and one for the fractional euro value.
Or you can use java.math.BigDecimal.
This issue keeps coming up. Check the forum for a recent thread about fractions.

Similar Messages

  • Float number rounding

    how can I determine whether the number of digits after decimal point is greater than one? For e.g., the float number of 16.04(or more digits) should be rounded, but the number of 16.1 should not be rounded.
    Is there any other convenience method except the split method in String class?
    thanks in advance.

    Yes, the formula you mentioned is what I'm using to round the float number to the tenth place.
    float f = ...;
    f = Math.floor(f*10) / 10;
    but the float number I'm choosing to be rounded should have the number of digit numbers after decimal point greater than one, for e.g.,
    f = 16.045 need to be rounded to 16.0 using that formula
    f = 16.01 ......
    f = 16.0 need not to be rounded since it aready just have one digit number after decimal point
    f = 15.9 ......
    sorry about the confusion. thanks for your help.

  • Round up a float number

    Hi!
    Which Math function do I have to use to round up a float number ? I thought the correct one was the Math.ceil() function but I don't get what I need. I mean, which is the correct function to get the following result:
    Math.function(2.75)=3
    Thanks

    This is what I get:
    Math.ceil(2.75)=2I get 3.0 for that calculation. You must be doing something besides the raw calculation. Here's my code that tests it:public static void main(String[] args) {
      System.out.println(Math.ceil(2.75));
    }Let's see yours.
    PC&#178;

  • How is it possible to precise a float number in java

    Hello ,
    It is my second post and i couldnt find an answer to it. The problem ii want to display a float number with only the last three decimal after the comma. For example, float x = 23.445566 and i need to display only 23.445
    How can i do that ? here is the code:
    int val1 = 1432;
    int val2 = 13;
    float result = 0.000F
    (float) (valu1/valu2)
    the result = 110.153846...
    And what i want to display is 110.153
    Please help.
    Thanks

    You're right - java.text.Decimal format uses round half even, java.util.Formatter uses round half up.
        Float f = 0.5F;
        System.out.printf("%.0f\n", f);
        System.out.println(new java.text.DecimalFormat("0").format(f));
    1
    0In the ASNI C implementations I've worked with, printf also uses half-even.
    Bizarre.
    Pete

  • Looking for VI where i can convert covert a float number to Q 11.5 representation

    looking for VI where i can convert covert a float number to Q 11.5 representation and what exactly  Q 11.5 representation means.
    Kindly help me on the same.
    Solved!
    Go to Solution.

    Appears to be right, but don't forget to round to the nearest integer.
    http://zone.ni.com/reference/en-XX/help/371361H-01/glang/round_to_nearest/
    Now is the right time to use %^<%Y-%m-%dT%H:%M:%S%3uZ>T
    If you don't hate time zones, you're not a real programmer.
    "You are what you don't automate"
    Inplaceness is synonymous with insidiousness

  • Error in mapping for floating Number calculation

    Hi All,
       I have a small doubt in floating number calculation in Mapping.
    Actually i am geting a floating point number and calculating the SUM and generating the output. The input is of 2 decimal places(Ex: 26.02  and 26.03 ), but when it is adding all the values it is generating a three digit decimal number (Ex: 52.050003)
    I dont know from where it is geting one extra number "2" in the output.
    Please find the code for the same and let me know if i need to do something else to get ride of this.
       //write your code here
    float sum=0;
    if(a != null && a.length > 0.00)
       for ( int j =0; j<a.length;j++)
        sum  =  sum + Float.parseFloat(a[j]);
       result.addValue(String.valueOf(sum));
    else
    result.addValue("0");
    Thanks in Advance,
    JAY

    Jay,
    Please use the below code and let us know, if it helps.
    BigDecimal sum= new BigDecimal("0");
    BigDecimal bd;
    if(a != null && a.length > 0.00)
    for ( int j =0; j<a.length;j++)
    bd=new BigDecimal(a[j]);
    sum=sum.add(bd);
    result.addValue(""+sum+"");
    else
    result.addValue("0");
    in import section - java.math.*;
    raj.
    Edited by: Raj on Feb 18, 2008 11:11 AM

  • How to get the amplitude of a sound as a float number in Java

    Hi, I would like to create a program which plays an audio file(it may be a midi, probably) and it returns
    the amplitude of the sound in terms of float number.
    I mean, is it possible to obtain this number, since every sound file is actually a sequence of float number like
    110.010110101 etc.
    Can you help me with suggestions and details about the way to take.
    Regards
    Frank

    For sampled sound, the answer might be seen in [ this thread|http://forums.sun.com/thread.jspa?messageID=10636576#10636576]. For MIDI it is different in that you will only be able to get it as sampled sound once it has been synthesized, and as I recently found out, to get the audio coming through the regular sound lines will take nothing less than a physcical or software 'audio loopback' being installed, on most PCs.
    Of course, if it is MIDI, you might just cheat by calculating a 'volume' from the note velocities in the MIDI score.

  • Converting string to float number

    Here's what I'm trying to do. The user types a number, such as 12.011, in an input box. I want to check to see if the number they typed is between two numbers, such as 12 and 12.1. Therefore, I want to convert the string in the input box to a float number, not an integer, so I can check to see if it's in the correct range. How do I convert a string into a float number?
    thanks
    Mark

    Did you try:
    Number(textinput1.text);
    if(Number(textinput1.text)>12 && Number(textinput1.text)<12.1)

  • Round up a number .

    Hi
    Can any one tell me how can i round up a number .
    for example :
    23.80   to 24
    23.30  to 23 .
    Regards
    Rakesh singh

    >Joy to the Workbench, the Search is come!
    >Let ABAP receive her Guru;
    >Let every thread, first search for it,
    >And search and search again,
    >And search and search again,
    >And search and search, and search again.
    >
    >Joy to the Search, the Round is found!
    >Let ABAPers their F1 deploy;
    >While fields and tables, functions and methods,
    >Round up and down with joy,
    >Round up and down with joy,
    >Round up, and down, and round with joy.
    >
    >No more let sins and guests grow,
    >Nor spam infest our second home;
    >Abuse report buttons, let them flow to mods,
    >Far as the curse is found,
    >Far as the curse is found,
    >Far as, far as, the curse is found.
    >
    >Still points-hunters come again,
    >And make of themselves a pain.
    >The repeated answers, go on and on and on,
    >Until the thread is locked,
    >Until the thread is locked,
    >Until, until... the thread... is... locked!

  • How to convert timing to floating number

    hi everyone, i want to do a time measurement of hall sensor and use this value to set a frequency and also to use as process value in a PID controller. the problem is that i get an dynamic data output and i'd like to have this in a floating number, is this possible? or another way is also good... hereby a jpeg to make it more clear.
    i also get more problems with this, when i change the throttle (0-100%) is doesn't do anything and my while loop also doesn't end anymore. this is just a small part of the complete program, maybe it's to heavy for my comp? (single core 1,6Ghz, 1,5gig ram) 
    hope it's a little bit clear what i mean
    thanks in advance ! 
    P.S: i know that the divide by one is useless because you can directly measure the frequency, this has allready changed.
           the program is meant to drive a PMSM motor 
    Solved!
    Go to Solution.

    hi thanks for the reply, the answer you gave helped me out.
    now i've noticed that when i ad the the pid loop (even when nothing of it is connected) the program doesn't respond anymore. is this because of my computer? the while loops (both the normal and pid loop) are stopped by a single switch...
    i have uploaded my VI so maybe someone could check it out, be aware it's totally not finished ^^. if you delete the pid control you'll see what i mean.
    thanks in advance 
    Attachments:
    PMSM steering.vi ‏2527 KB

  • How to transfer a float number to boolean

    I want to transfer a float number to a boolean array? How can I achieve?
    Solved!
    Go to Solution.

    YoungHang wrote:
    but actually "number to boolean array" can only transfer a integer or fixed-point number to boolean array , not float number
    Just typecast the DBL to U64 first.
    LabVIEW Champion . Do more with less code and in less time .

  • Float number

    Hi,
    I want to display the float number, when i use the numeric indicator, it only display the interger. how can i find the float number.
    thanks
    Attachments:
    ex1.jpg ‏961 KB

    Right click on the control or terminal, then select "Representation..."
    Chilly Charly    (aka CC)
             E-List Master - Kudos glutton - Press the yellow button on the left...        

  • Rounding a float to an integer in this program

    Hello, I would like to be able to use floats in this program. If i input say 50.5 at the moment i get an error (the program compiles ok).
    What I want to happen is that i enter say 50.5, the program rounds this to 51 and then uses 51 thus not getting an error.
    Thanks anyone
    *Write a program to prompt the user to enter a mark as a
    *floating point number.  Check that the mark is in the
    *range 0.0 to 100.0 and, if not, repeat the prompt until
    *the mark is in the required range.  Then display the
    *indicative degree classification according to the mapping
    *above, bearing in mind that marks with fractional parts
    *greater than .5 are rounded up (e.g. 59.5 is to be interpreted
    *as 60 and so as an Upper Second).
    *Test your program to ensure that the mapping is correct.
    import java.util.Scanner;
    class FloatMark
         public static void main(String[] argStrings) throws Exception
              float mark;
              int intMark;
              do
                   Scanner scan = new Scanner(System.in);
                   System.out.println("Give me the float mark please: ");
                   mark = scan.nextInt();
                   intMark = Math.round(mark);
                   if(intMark < 0 || intMark > 100)
                        System.out.println("Give me a valid float mark please");
                   else if (intMark < 40)
                        System.out.println("Fail!");
                   else if (intMark >= 40 && intMark < 49)
                        System.out.println("Third");
                   else if (intMark >= 50 && intMark < 59)
                        System.out.println("Lower second");
                   else if (intMark >= 60 && intMark < 69)
                        System.out.println("Higher second");
                   else if (intMark >= 70)
                        System.out.println("First");
              while(intMark < 40 || intMark > 0);
    }

    This is the latest and greatest for you, woo!
    import java.util.Scanner;
    class FloatMark
         public static void main(String[] argStrings) throws Exception
              float mark;
              int intMark;
              String exit = null;
              do
                   Scanner scan = new Scanner(System.in);
                   System.out.println("Give me the float mark please: ");
                   mark = scan.nextFloat();
                   exit = scan.nextLine();
                   intMark = Math.round(mark);
                   if(intMark < 0 || intMark > 100)
                        System.out.println("Give me a valid float mark please");
                   else if (intMark < 40)
                        System.out.println("Fail!");
                   else if (intMark >= 40 && intMark < 49)
                        System.out.println("Third");
                   else if (intMark >= 50 && intMark < 59)
                        System.out.println("Lower second");
                   else if (intMark >= 60 && intMark < 69)
                        System.out.println("Higher second");
                   else if (intMark >= 70)
                        System.out.println("First");
              while(intMark < 100 || intMark > 0 || exit != "e");
    }

  • How to Enter Floating number in Modbus Register

    Hello,
    I want to set one floating value in modbus register located on 400.but I am not able to do so its only accept real number.
    Will you please guide me what should I do in order to solve this problem??
    I tried with suggestion which is given in link but still I am not able to connect the output to the modbus which I have mark red in attachment.
    http://digital.ni.com/public.nsf/allkb/AD8172B710AFB13F86257974007D6F52
    Actually I want to set my actual current value same as  measured value that's why I need to do this operation.
    Thank you very much in advance. 
    Attachments:
    internal current.png ‏416 KB

    The best way what i found is multiply  the number by 100 if after decimal 2 digit precision is OK with you other wise 1000 if 3. digit precision. Then write to register.
                                                                   Later when reading read & divide by 100 or 1000 whatever 
    Kudos are always welcome if you got solution to some extent.
    I need my difficulties because they are necessary to enjoy my success.
    --Ranjeet

  • Rounding a float value

    totalamount = totalamount + Float.parseFloat(amount1);
    From the above code I am getting totalamount which is of float datatype. The value has more than two decimals like 123.23232 I need to round it to 123.23. How could this be done ? Any help is appreciated.

    You can do the following to round to two figures after the decimal point, rounding to the nearest neighbor, if they are equidistant, round up:
    totalamount = totalamount + Float.parseFloat(amount1);
    BigDecimal bigDecimal =new BigDecimal(totalamount ).setScale(2,bigDecimal.ROUND_HALF_UP);
    I hope this helps,
    Val.

Maybe you are looking for