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.

Similar Messages

  • Rounding up of a float value

    i need some help in rounding up the float value to two places after the decimal point
    i have a value like 25.34446789 and i need to round it up like 25.34

    i tried this function but i am not getting the result
    correctly .if i am using the round () function i am
    getting a value like 55.0 when the original value is
    55.085673.
    i want to get it like 55.09. round to two places
    after the decimal point .is there any other way to do
    it ?How about doing what Chuck said you should do? Give it a try and think about what he wrote. It may actually work.

  • Rounding ist wrong for float value (MS SQL 2005)

    Hello
    I have a simple report with a command:
    select num = convert(float, 4.145)
    Field round over "Format Field" or ToText( num, 2) the result is 4.14  -> Wrong
    if  i use Round(num, 2)  result is 4.15 --> OK
    In CR 8.5 result is always correct
    Thx

    I know the problematic of the floating values.
    My big trouble is the inconsistency in the report!
    It can't be that the formula function Round(x,y) show another result than the integrated field-rounding function.
    Also in the formula that ToText(x, y) is different to Round(x,y).
    Following another stupid difference:
    Command.num = 4.145          Result: TRUE
    Command.num - 4.145 = 0     Result: FALSE
    Mathematical it's the same.
    CR8 was consistence, after migration we have different result on our reports!

  • Limit a float value to single decimal place after round off

    41.231445 is float value . How can I limit this to a single decimal place after round off?
    ie; 41.2
    Any help in this regard will be well appreciated with dukes.
    Regards,
    Anees

    Also read this: [http://java.sun.com/docs/books/tutorial/i18n/format/decimalFormat.html]

  • Logging float values in a MySQL : wrong fromatting

    Hi,
    I'm using TS with standard DB schema for MySQL to log results from my tests.
    Some of the test are standard numeric limit tests (no adapter). Limits and data source are float numbers.
    Most of the time everything is well logged in the MySQL DB, but sometimes the floating value is rounded !
    You can see attached the result in the database and the value from the result which has been logged. In the resultList the data is weel formatted but in MySQL it's rounded.
    The screen copy shows several numeric limit tests, these numeric limits are in fact the same step which is called at several moments in my sequence. So it's not a problem of result formatting for specific test. However it seems that it happens when the limits (coming from a sequence parameters) are set below 1.
    What can cause this behavior ?
    BTW, I'm using TS2012 and MySQL 5.2 (ODBC 5.2.5.0 ANSI Driver).
    Attachments:
    MySQLResults.PNG ‏50 KB
    ResultList.PNG ‏21 KB

    Hi zyl7,
    The issue is most likely related to the way floating point values are stored in MySQL. To quote MySQL documentation, "Floating-point numbers sometimes cause confusion because they are approximate and not stored as exact values. A floating-point value as written in an SQL statement may not be the same as the value represented internally".
    There are more details regarding this in the following link:
    http://dev.mysql.com/doc/refman/5.1/en/problems-with-float.html.
    I have tried to reproduce your scenario using TS 2012 sp1 and MySQL 5.2.5.0 ANSI ODBC Driver but I was unsuccessful even after trying a range of similar values. So the issue you faced is most likely dependent on the machine in use and might not be reproducible in a different set-up.
    If the reason you raised this concern is because any comparisions on the floating point values stored in the database might be incorrect (because of the differences in the value passed from TestStand and the actual value stored), please follow the guidelines specified in the link above (especially the comments in the end of the page) to ensure your comparision operations always use a tolerence when calculating the results.
    TestStand does float comparisions using a tolerence, so the pass/fail status generated by TestStand should be accurate.
    Let me know if you have any concerns.

  • Problem in storing float value into database form C++ thru OCCI

    Hi,
    We are working on a project where numerical accuracy is crucial. As per our client required float data like 23.12345678 should be stoted and retrieved back accurately from C++ application.
    We are using OCCI interface in C++ application. At the time of executing the query, we are moving the float value to host variable using setFloat() function. But oracle storing truncated or rounded value. Not exactly what we are proving.
    Even we are not able store 22.22 also properly. In oracle column is defined as NUMERIC.
    Its working fine, when we insert data from sql command promt by running a query. I guess we are missing some this in this process.
    Please provide us any suggestions on this. It would be great if you can provide some sample code.
    Regards,
    Newbe

    OTN has a C/C++ forum. Please delete this post and post your inquiry there.
    Thank you.

  • Union two tables with diffrent count of fields with null and float value

    Hello,
    i want to union two tables, first one with 3 fields and second one with 4 fields (diffrent count of fields).
    I can add null value at end of first select but it does not work with float values in second table. Value form second table is convert to integer.
    For example:
    select null v1 from sessions
    union
    select 0.05 v1 from sessions
    result is set of null and 0 value.
    As workaround i can type:
    select null+0.0 v1 from sessions
    union
    select 0.05 v1 from sessions
    or simple change select's order.
    Is any better/proper way to do this? Can I somehow set float field type in first select?
    Best regards,
    Lukasz.
    WIN XP, MAXDB 7.6.03

    Hi Lukasz,
    in a UNION statement the first statement defines the structure (number, names and types of fields) of the resultset.
    Therefore you have to define a FLOAT field in the first SELECT statement in order to avoid conversion to VARCHAR.
    Be aware that NULL and 0.0 are not the same thus NULL+0.0 does not equal NULL.
    In fact NULL cannot equal to any number or character value at all.
    BTW: you may want to use UNION ALL to avoid the search and removal of duplicates - looks like your datasets won't contain duplicates anyhow...
    Regards,
    Lars

  • Need some help in Rounding a double value to a whole number

    Hey Peeps,
    Need some help here, I got a method that returns a value in double after a series of calculation.
    I need to know how can I round the double value, so for example,
    1. if the value is 62222.22222222, it rounds to 62222 and
    2. if the value is 15555.555555, it rounds to 15556
    How can i do this
    Zub

    Hi Keerthi- Try this...
    1. if the value is 62222.22222222, it rounds to 62222 and
    double d = 62222.22222222;long l = (int)Math.round(d * 100); // truncatesd = l / 100.0;
    double d = 62222.22222222;
    System.out.println(d);
    long l = (int)Math.round(d * 100);
    // truncatesSystem.out.println(l);
    d = l / 100.0;System.out.println(d);
    for (int i = 0; i < 1000; i++)
    {    d -= 0.1;}
    for (int i = 0; i < 1000; i++)
    {    d += 0.1;}System.out.println(d);
    regards- Julie Bunavicz
    Output:
    62222.22222222
    62222
    62222.22
    62222.22000000000001

  • How to extract an integer or a float value from a String of characters

    Hi i have a problem getting to a float value within a string of characters..
    for instance the string is
    "numberItem xxxxxxxxx 700.0" (each x is a space..the forum wouldnt let me put normal spaces for some reason)
    how do i store 700.0 in a float variable
    remember the string is obtained from an inputfile
    so far i got the program to store the inputfile data line by line in a String array..i tried tokenizing the string to get to the the float value but since i have mulitple spaces within my string, the token method only gets "numberItem" from the above String
    This is all i have so far:
    String c;
    String Array[] =new String[g]; (i used a while loop to obtain g(the nubmer of lines in the file))
    while((c=(cr.readLine()))!=null)
    Array[coun]=c;
    it would be reallllllllllllllllllllllllllllllllllllllly easy if there was a predefined method to extract numeric values from a string in java..
    Edited by: badmash on Feb 18, 2009 5:50 PM
    Edited by: badmash on Feb 18, 2009 5:50 PM
    Edited by: badmash on Feb 18, 2009 5:55 PM

    badmash wrote:
    Hi i have a problem getting to a float value within a string of characters..
    for instance the string is
    "numberItem xxxxxxxxx 700.0" (each x is a space..the forum wouldnt let me put normal spaces for some reason)
    with the space included
    how do i store 700.0 in a float variable
    remember the string is obtained from an inputfile
    so far i got the program to store the inputfile data line by line in a String array..i tried tokenizing the string to get to the the float value but since i have mulitple spaces within my string, the token method only gets "numberItem" from the above StringHuh?
    Not true.
    Anyway why not use string split, split on spaces and grab the last element (which by the format you posted would be your 700.0)
    Then there is the Float.parseFloat method.
    It is easy.
    And another thing why not use a List of Strings if you want to store each line? (And why did you post that code which doesn't really have anything to do with your problem?) Also in future please use the code formatting tags when posting code. Select the code you are posting in the message box and click the CODE button.

  • How do i convert a float value to a double value?

    How do i convert a float value to a double value? HELP PLEASE!! im very stuck!! i gota float data type and i need to convert it to a double data type in order to use it in another operation.....
    thank u so much!

    safe dint realise ppl were so arrogant. thanks for the reply but less of the sarcasm!

  • Rounding of a value to 2 decimal points.

    Hi all,
    I need to round off a value to 2 decimals.
    For example:
    A value 13.6789 should be rounded of to 13.67 and not 13.68

    Hi,
    Thats Truncation not rounding, for data types P and F we cannot have sub field access,
    so we cannot use offset, it leads to error results
    and if we use
    data : var type p decimals 2     they will be rounded hence  
    So, pass it to   a character variable
    and use write satement edition
    example
    data : var(7) type p decimals 4  value '13.6789 ' .
    write /  var.
    data : var1(8) type c.
    move var to var1.
    write /(5)  var1.
    regards
    prasanth

  • How can i write the floats value in Unitronics vision230 plc using modbus Ethernet

           How can i write the Float value in unitronics Vision230 PLC usinsg modbus ethernet (MB Ethernet Master Query.vi) I  read and write  the 32 bit register,  for e.g i want to write the 23.45 value on 2nd add. of MF. And MF register is 32 bit register. I  read and write  the 32 bit register.
    Narendra.
    Solved!
    Go to Solution.

     Thanks Amit for your solution but i can not use the string to write the value because  MB Ethernet master Query.vi only accepet the integer value its not take string values or any other i.e floats values etc.....otherwise i have  no problem to write or read the 32 bit register values , only problem is that the MB Ethernet master Query.vi only accept the integer value there4 how can write the float value.
    Narendra
    Message Edited by Artemistech on 01-30-2009 11:06 PM

  • Rounding up off values using Different Condition types

    Dear All,
    There is a requirement from the client where they want to round up the values in all the condition type values.For Example:
    ZPR0-           1525
    ZCOM(15%)-  228.75(It Should be 229.00)
    NBT(Tax2%)   25.93(It Should be 26)
    Similarly all the values for the condition should be calculated as the above mentioned scenario .Kindly suggest
    Thank You!
    Chakradhara

    1.In V/06 goto the respective conditions type and in rounding rule field maintain A
    2.goto pricing procdure in v/08 in calculation type field maintain routine 17 against the condition type s
    3.in OB90 maintain 100( rounding unit) against your company code then check the results.
    Thanks
    Srinu.

  • Function module to convert float value to data type 'dec'

    Hi experts,
      In a report i need to convert float value to the data type 'DEC'. How to convert it. Is there any function module for this conversion.
    Thanks and Regards,
    Vaibhav Tiwari.

    Hi ..
    We can do like this...
    Data : V_float type F value '12345.67'.
    Data: V_dec type P Decimals 2.
    Write:/ V_float exponent 0. "This will display it like Type P
    or
    Write V_float to V_dec EXPONENT 0.
    Write:/ V_dec.
    reward if Helpful.
    <b></b>

  • Round up my values in DATA FORM

    Hi
    Is there any chance i could round up my values in data forms?
    245.587 = 246
    Thanks

    Have you had a look at the precision settings for a form - http://download.oracle.com/docs/cd/E17236_01/epm.1112/hp_admin/frameset.htm?form_opt.html
    Otherwise you could have a business rule that rounds the values using the @ROUND function - http://download.oracle.com/docs/cd/E17236_01/epm.1112/esb_tech_ref/round.html
    Cheers
    John
    http://john-goodwin.blogspot.com/

Maybe you are looking for