Rounding off a float to two decimal places
I want to make a function where I shall pass a float and and integer.
The float shall have to be rounded off to a value in the integer.
Can anyone please suggest how to round off a float.
E.g.: if the float is 12.56890 and I want to round it off to 2 decimal places, then it should be 12.57.
Regards
Hawker
I didn't mention any datatypes like float, double.True, but that is what the question is about, so you weren't answering the question. For a change.
As I mentioned, that was just a mathematical steps to round of the floating point value. (Not in any programming languages point of view).False. You didn't mention that at all.
This is the code for that in java.So here you are mentioning datatypes and floats for the same piece of mathematics that you have already been told, with reasons, doesn't work in floating point.
which seems to be working fine
Seems to. What evidence do you have that the float actually got rounded? As opposed to got displayed as rounded? Which is not what the OP asked for.
And of course all that code seems to do is round 0.01 to two decimal places, which again is not what the OP asked for.
For any remaining fans of this 'technique', please explain the behaviour of the following code:
public static void main(String[] args)
int count = 0, errors = 0;
for (double x = 0.0; x < 1; x += 0.0001)
count++;
double d = x;
int scale = 2;
double factor = Math.pow(10, scale);
d = Math.round(d*factor)/factor;
if ((d % 0.01) != 0.0) // if 'd' really has been rounded this should be zero
System.out.println(d % 0.01);
errors++;
System.out.println(count+" trials "+errors+" errors");
}
Similar Messages
-
Any function module to round of a value to two decimal places?
Hi gurus,
Any function module to round of a value to two decimal places?Hi,
you can use FM 'ROUND'.
Thanks. -
Is it possible to convert a float to two decimal places
Can anyone help me to convert a float to two decimal places
float f=16;
System.out.println(f);
It will print 16.0
But I want to get printed as 16.00"convert" no, as no conversion is needed, obviously. Format the output, yes. Read the API for String and its printf method.
-
Using float to two decimal places
yes its me once again....
a human planet earth
ok......project coimng to a finish..... thank you to all who have responded to previous messages posted.... hopefully this may be the last... for a while anyway.....
var1 successfully parsed text to float
var2 successfully parsed text to float
float var3= var1 * var2
display of var1 2 and 3 ok
but need to reduce/control the number of decimal places being displayed.
in 'C' its easy but havent been able to find anything as yet under Java....
any pointers appreciated(sigh...) java.text.DecimalFormat
-
Rounding off Amount which has 3 decimal places
Dear All ! !
Urgent Help required i need to round the Amount of 3 decimal pleaces for example
300.777 should be round to 300.800
300.340 should be round to 300.300
or
300.977 should be rounded to 301.000
Payroll in international for Bahrain and Oman both currency value is too high but they use to round after decimal. Can any one Guide any function any operation or some other solution will be highly regarded
Thanks
Syed BaddarHi
You can do this by writing a rounding calculation rule.
Or ask an ABAPer to write a simple routine in payroll driver program RPCALCX0. It has an exit where he/she can write a small routine for this.
Regards
Waz -
Rounding off float to 2 decimal places and returning the float
I tried doing bigdecimal but it doesnt work. :
BigDecimal bd = new BigDecimal(5 * 0.0394);
retval = bd.floatValue();
System.out.printf(" th %f\n",retval);
I see 0.197000. I know that there are 6 places since I didnt do %.2f. However its not the printf thats important . The float I want to return must be rounded off to two decimal places.
Thanks in advance for the help,
sbUse BigDecimal.round( MathContext mc ), where the MathContext object is set to a precision value of 3;
BigDecimal roundedBigD = myOriginalBigD.round( new MathContext( 3 ) );� {� -
Price per unit is getting round off to two decimal places.
Hi Gurus,
In the Billing output, the Price per unit is getting round off to two decimal places.
How can we avoid this or allow 4 decimal places for such cases.
In the same pricing procedure, we have two pricing condition types PR02 and ZR01.
When PR02 is being used,it doesnt rounds off Price per unit.e.g .578888 = .578888
But when ZR01 is being used the system rounds off price per unit.e.g.578888 = .58
Please suggest where is the setting for it.
Thanks
MonteeHi,
Which currency you are using? If its INR, then it will do only for 2 decimal places.
The solution for ur problem can be ractified by changing the scales :
Also check OSS 80183
Thanks,
Raja -
Round off to two decimal places
hi
i have a filed in it values are there i want to round off to two decimal places
which is the function module for it please suggest
regards
AroraHi Nishant Arora,
Please check this code.
DATA: N1 TYPE P DECIMALS 4 VALUE '0.0565',
N2 TYPE P DECIMALS 2.
MOVE N1 TO N2.
WRITE: N2.
Here the value is get rounded to 0.06.
Rather than function modules you go with this. I think it is easier.
Reward points if useful.
Cheers,
Swamy Kunche -
Rounding off to two decimal places
Hello,
For simplicity, is there a method which will round off
a double variable to two decimal places.
Excample: if I have a result which equals 2.1999998
and I want to display this as 2.20 in a TexTField.
Any help would be much appreciated.
ThanksIf you are trying to do dollars and cents, I further recommend you use:NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance();
String currencyOut = currencyFormatter.format(yourNumber);to format your numbers as they will give the correct precision for international currencies simply by setting the locale, and they will also automatically handle the currency symbols ($, DM FR, etc.)
Doug -
Rounding to two decimal places
In Java, how do I round a number off to two decimal places?
Thank Youjava.text.DecimalFormat
Or use java.math.BigDecimal and specify rounding rules, apparently. -
Rounding to two decimal places in BPC/NW
Hello,
I am dealing with the task to round values to two decimal places. I have found some discussions in BPC-MS where the statement ROUND should work in logic. Unfortunately I am not able to make it work in the NW version.
Isn't there also a parameter in the appset or application administration which would cut the numbers only to two decimal places (this task would be also a possible solution for us).
Thanks for any help
JanHello Pravin,
the second import worked fine. I realized that the class has changed to ZCL_BPC_SL_ROUND.
I debugged the class and noticed that the parameter it_cv is not filled at all. Therefore the program ends before starting the rounding procedure. Here is the part of the code:
Take CV into account
clear l_success.
loop at it_cv into ls_cv.
clear l_val_string.
loop at ls_cv-member into l_member.
if sy-tabix eq 1.
l_val_string = l_member.
else.
concatenate l_val_string l_member into l_val_string separated by ','.
endif.
endloop.
if l_val_string ne space.
l_success = add_dim_restriction( i_param = l_val_string i_dimension = ls_cv-dimension i_clear = abap_true ).
if l_success eq abap_false.
concatenate 'Failed to successfully add Dimension' ls_cv-dimension into l_log_msg
separated by space.
cl_ujk_logger=>log( l_log_msg ).
cl_ujk_logger=>log( 'Exiting Round Method' ).
cl_ujk_logger=>empty_line( ).
raise exception type cx_uj_custom_logic
exporting messages = et_message.
exit.
endif.
endif.
endloop.
if l_success eq abap_false. exit. endif. - here the program exits and therefore doesn't do any rounding.
If I comment this part the system rounds correctly but runs on all records in the cube which degrades performance a lot.
Could you please advice further if it is possible to restrict the data region only to the submitted data?
Best regards
Jan -
Storeing values in floate data type upto two decimal places
Hi
I want a float type field in my table but want to store value up to two decimal places.
so what syntax i need to follow plz suggest.
thanksit is not possible to store with Number datatype. if you are using the data to sho report, then change its format (like 45 to 45.00). if you want to store 45.00 in database, then you have to modify the datatype for that column. for this follow the following path
1. Add a column of datatype Varchar2.
2. Copy all the value of your number field to it.
3. Drop the old column.
4. However make sure that if you have used that column somewhare in arithmetic calculation, the modify it and use to_number function to do arithmetic opration. -
How to take float precision to two decimal places?
Hi, everybody:
I'm a straight noob...first week in Java programming. Before you bash me for not checking FAQs and such, believe me, I have.
I have tried BigDecimal movePointLeft, but I don't know how to construct it using proper syntax. I also tried currency and toString but I keep messing up my syntax somehow. Four hours into this and nothing is working.
Here is the problem: I have a float. I want to format its output to two decimal places, trying to achieve in Java what C would produce as:
printf("The amount you owe is $%.2f", fltAnyName);
Any help would be appreciated.
Thanks,
RobSorry, guys. I have tried both recommendations and I can't get either to work. I'm getting a compile error pointing to the delineating period between the "out" and "printf" when I try the C-like code. I have 1.5, too. I know your advice is sound so I must be doing something wrong. Can you insert the code DecimalFormat code that will correct this problem where it needs to go? The packages below have been included becasue of all the different solutions I have attempted.
import java.io.*;
import java.math.*;
import java.text.*;
import java.lang.*;
public class CoinCalculator2
public static void main(String[] args) throws IOException
String strQuartes, strDimes, strNickels, strPennies;
int intQuartes, intDimes, intNickels, intPennies;
float fltValue;
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
System.out.println("THE COIN CALCULATOR");
System.out.println();
System.out.print("Enter the number of quarters you have: ");
strQuartes = dataIn.readLine();
intQuartes = Integer.parseInt(strQuartes) * 25;
System.out.print("Enter the number of dimes you have: ");
strDimes = dataIn.readLine();
intDimes = Integer.parseInt(strDimes) * 10;
System.out.print("Enter the number of nickles you have: ");
strNickels = dataIn.readLine();
intNickels = Integer.parseInt(strNickels) * 5;
System.out.print("Enter the number of pennies you have: ");
strPennies = dataIn.readLine();
intPennies = Integer.parseInt(strPennies);
fltValue = (intQuartes + intDimes + intNickels + intPennies) / 100;
System.out.println();
System.out.println("The value of your coins is $" + fltValue);
System.out.println();
} -
How do I round calculations to two decimal places (form-wide)
Hi,
I've created an order form where customers input the quantity of product they want to order and the form outputs the total due (including shipping and sales tax). Depending on user input, the resulting total sometimes contains more than two decimal places. How do I round calculations to two decimal places? Is there a way to do this form-wide?
I'm new at using Acrobat for calculations. Any help would be greatly appreciated! Thanks...The above will affect the displayed value not the actual value of the field nor its value when accessed in another computation. This behavior may cause an error of 1 cent or more in the grand total or sales tax computation. If you want the the value and displayed value to be the same value you can use the following Validation script:
event.value = util.scand("%,1 0.2f", event.value); -
Help!printing float values with two decimal places
hi there java pips! im a newbie to this technology so forgive me for this really stupid question....i would like to perform mathematical operations on two float values....the problem is i want to print them in a standard format and that is i want them to be displayed with two decimal places (e.g 190.00, 12,72, 1,000.01) how can i do this?
Try java.text.DecimalFormat
NumberFormat nf = new DecimalFormat("0.00");
System.out.println(nf.format(x));
Maybe you are looking for
-
Trying to transfer ringtones at my apple store list of ring tones but when i plug my phone into my pc they are not showing up on my phone? Help
-
Proforma Invoice (F8) for Stock Transport Order
With regard to intra-company STO, we have a specific requirement: Referencing delivery document generated using Xn: VL10B (delivery from Stock Transport Order), we create proforma invoice type F8. Subsequently, excise invoice is created referencing t
-
Can I move music from broked ipod to other ?
I have 2 ipod. 1 is broked-crashed with muy music, and I can't slide to unlock. Can I move that music to other one ???
-
Purchase with Letter of Credit
Hi Experts.. Can we manage purchase transaction with letter of credit in SAP Business One? Thank you.
-
Question about heat & system fan ? with Corecenter software?
hello guys, i am having some strange experience well i just happened to clean my CPU well just external clean with cloth well then when i started i heard sound of some sort it turned out to be a plastic piece with the fan anyway i jus