Round to two decimal places...and keeping trailing 0
Ok, I have done a search on rounding. So I made this function:
private double round(double number){
double d = Math.pow(10, 2);
return Math.round(number * d) / d;
}However, if I use a number like 3.59999, it returns 3.6. For what I am using it for, I need it to return 3.60. Any ideas? Thanks.
You normally only format for display! You seem to be wanting the default format to show 2 decimal places. This makes no sense. Internally a number(double or float) doesn't know about decimal places so how can the default format know you want 2 decimal places?
For the most part you only need to worry about decimal places when you print a number using the DecimalFormat class.
There are times when you might want to round values to 2 decimal places before doing more calculation. I have only met this when adding monetary values when it is important that a column total equals the sum of a displayed column values. In this case one uses something like the technique you originally proposed to round values before summing them. You then use DecimalFormat to display the values.
Similar Messages
-
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 -
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. -
Hi Friends
i need to do rounded to two decimal value of required view attribute(messageStyleText).
How can i get the value-> round the value ->set this rounded value in same view attribute again.
This thing planing to do by extending the CO.
I extended the VO but its not working...
can anybody suggest how can we do this in controller..
Regards
--HarryHi Anil,
Thanks for your reply..
I tried the below code..but its not seting the value
code:
if(pageContext.getParameter("EBtn") != null)
OAViewObject vo =
(OAViewObject)am.findViewObject("UpdateVO");
String ss1 = vo.getCurrentRow().getAttribute("ECost").toString();
pageContext.writeDiagnostics(this,"@123ECost Value is: " +ss1,1);
double d = Double.valueOf(ss1).doubleValue();
DecimalFormat df = new DecimalFormat("#0.00");
String ss2 = df.format(d).toString();
pageContext.writeDiagnostics(this,"@12345Estimated Value with 2 decimals: " +ss2,1);
vo.getCurrentRow().setAttribute("ECost",ss2);//its not seting the value
//I tried the below code also..but no use
/*OAMessageStyledTextBean oastb = (OAMessageStyledTextBean)webBean.findChildRecursive("WOEstimatedCost");
oastb.setValue(pageContext,ss2);*/
Can any one please help me out
Regards
--Harry -
my cost per month textbox typically returns multiple decimal places, is it possible to limit it's output to two decimal places like real monetary calculations?
calculatebutton.addEventListener(MouseEvent.CLICK, doMyCalculation);
calculatebutton2.addEventListener(MouseEvent.CLICK, doMyCalculation2);
costpermonth.restrict = ".0-9";
var myAnswer : Number;
myAnswer = Number(costofitem.text) - Number(amountsaved.text);
amountneedtosave.text = String(myAnswer);
function doMyCalculation2(e:MouseEvent):void
var myAnswer2 : Number;
myAnswer2 = Number(amountneedtosave.text) / Number(howmanymonths.text);
costpermonth.text = String(myAnswer2);use the toFixed() method of numbers:
amountneedtosave.text = myAnswer.toFixed(2); -
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 -
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); -
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
HawkerI 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");
} -
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 -
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. -
Rounding a number upto two decimal places.
Hey !
My question is regarding the rounding of a number in such a way that answer that we get after the rouning comes upto two decimal places,no matter how small or the large the number before rounding was.
Somebody please help me.
thankshi,
use BigDecimal class in java.math package.
here is some code how to do it ..
BigDecimal bd = new BigDecimal(your number here);
bd.setScale(2,BigDecimal.ROUND_DOWN)
System.out.println(bd);
hope this helps ...
bye
ashok -
Double value truncated to two decimal places without rounding the value.
I want to truncate double value to two decimal places without doing the rounding of the value.
Is there any method which can directly do the truncation.There's many ways to achieve this such as using
BigDecimal's setScale method or type-casting. This is
the way I like to do it:double d = -5.239;
d = d > 0 ? Math.floor(d * 100) / 100.0 : Math.ceil(d
* 100) / 100.0;
Your division by 100 may cause an rounding error, because there are numbers which no finite binary representation. That's splitting hairs! I know ;-) -
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 -
To remove decimal places and round
hi all ,
select (sum(case when (F.ITEM_UOM = 'Cubic Feet' and upper(substr(H.SLURRY_TYPE,1,3)) <> upper('Top')) then
(((((H.SLURRY_VOL_ACTUAL * 5.6146)/H.SLURRY_YIELD)*94)/2000) )
when (F.ITEM_UOM = 'Cubic Feet' and upper(substr(H.SLURRY_TYPE,1,3)) = upper('Top')) then
( ((nvl(H.CLASS_V_CEMENT,0)*94)/2000) )
when F.ITEM_UOM = 'Pound' then
CEIL( ROUND((((((H.SLURRY_VOL_ACTUAL * 5.6146)/DECODE(H.SLURRY_YIELD,0,NULL,H.SLURRY_YIELD)))*94)*F.ITEM_PERCENT/100)))/2000
end)*(j.opn_value/2)) SUM_MAT_ACT_TON ,--------transport
sum(case when (F.ITEM_UOM = 'Cubic Feet' and upper(substr(H.SLURRY_TYPE,1,3)) <> upper('Top')) then
((H.SLURRY_VOL_ACTUAL * 5.6146)/H.SLURRY_YIELD)
when (F.ITEM_UOM = 'Cubic Feet' and upper(substr(H.SLURRY_TYPE,1,3)) = upper('Top')) then
nvl(H.CLASS_V_CEMENT,0)
when F.ITEM_UOM = 'Pound' then
ROUND(((ROUND(((H.SLURRY_VOL_ACTUAL * 5.6146)/DECODE(H.SLURRY_YIELD,0,NULL,H.SLURRY_YIELD)))*94)*F.ITEM_PERCENT/100 )/P.ATTRIBUTE5)
end) SUM_sload , ----sload
sum(case when (F.ITEM_UOM = 'Cubic Feet' and upper(substr(H.SLURRY_TYPE,1,3)) <> upper('Top')) then
((H.SLURRY_VOL_ACTUAL * 5.6146)/H.SLURRY_YIELD)
-- when (F.ITEM_UOM = 'Cubic Feet' and upper(substr(H.SLURRY_TYPE,1,3)) = upper('Top')) then
-- nvl(H.CLASS_V_CEMENT,0)
when F.ITEM_UOM = 'Pound' and F.BLEND <> 'PH'then
ROUND(((ROUND(((H.SLURRY_VOL_ACTUAL * 5.6146)/DECODE(H.SLURRY_YIELD,0,NULL,H.SLURRY_YIELD)))*94)*F.ITEM_PERCENT/100 )/P.ATTRIBUTE5)
end) BLENDING
,e.opn_job_desc
FROM xxnp_opn_joblog_001 E,
XXNP_OPN_JOBLOG_EST_002 F,
XXNP_OPN_JOBLOG_STAGE_002 G,
XXNP_OPN_JOBLOG_SLURRY_003 H,
XXNP_OPN_JOBLOG_RES_005 J,
MTL_SYSTEM_ITEMS_B P
WHERE E.OPN_JOB_DESC = 'K/D/SP158/SA399/1338/D/0510/1' AND
E.MANUAL='N'
and J.opn_resource_desc='4X4 PICK-UP OR LIGHT VEHICLES'
AND E.OPN_JOBLOG_001_ID = J.OPN_JOBLOG_001_ID
and F.INVENTORY_ITEM_ID=P.INVENTORY_ITEM_ID
AND p.ORGANIZATION_ID='103'
AND E.OPN_JOBLOG_001_ID = G.OPN_JOBLOG_001_ID
AND G.OPN_JOBLOG_006_ID = H.OPN_JOBLOG_006_ID
AND H.OPN_JOBLOG_007_ID = F.OPN_JOBLOG_007_ID
AND G.OPN_JOBLOG_006_ID = H.OPN_JOBLOG_006_ID
AND H.OPN_JOBLOG_007_ID = F.OPN_JOBLOG_007_ID
GROUP BY E.OPN_JOB_DESC,j.opn_value
o/p i am getting
SUM_MAT_ACT_TON SUM_SLOAD BLENDING OPN_JOB_DESC
11587.7954154787 1957.62401925833 1454.62401925833 K/D/SP158/SA399/1338/D/0510/1
well can anyone guide me on obtaining the following output (to remove decimal places and round )
SUM_MAT_ACT_TON SUM_SLOAD BLENDING OPN_JOB_DESC
11588 1958 1455 K/D/SP158/SA399/1338/D/0510/1thanking in advance
Edited by: makdutakdu on May 25, 2010 12:48 PMselect round( (sum(case when (F.ITEM_UOM = 'Cubic Feet' and upper(substr(H.SLURRY_TYPE,1,3)) upper('Top')) then
(((((H.SLURRY_VOL_ACTUAL * 5.6146)/H.SLURRY_YIELD)*94)/2000) )
when (F.ITEM_UOM = 'Cubic Feet' and upper(substr(H.SLURRY_TYPE,1,3)) = upper('Top')) then
( ((nvl(H.CLASS_V_CEMENT,0)*94)/2000) )
when F.ITEM_UOM = 'Pound' then
CEIL( ROUND((((((H.SLURRY_VOL_ACTUAL * 5.6146)/DECODE(H.SLURRY_YIELD,0,NULL,H.SLURRY_YIELD)))*94)*F.ITEM_PERCENT/100)))/2000
end)*(j.opn_value/2))) SUM_MAT_ACT_TON ,--------transport
round(sum(case when (F.ITEM_UOM = 'Cubic Feet' and upper(substr(H.SLURRY_TYPE,1,3)) upper('Top')) then
((H.SLURRY_VOL_ACTUAL * 5.6146)/H.SLURRY_YIELD)
when (F.ITEM_UOM = 'Cubic Feet' and upper(substr(H.SLURRY_TYPE,1,3)) = upper('Top')) then
nvl(H.CLASS_V_CEMENT,0)
when F.ITEM_UOM = 'Pound' then
ROUND(((ROUND(((H.SLURRY_VOL_ACTUAL * 5.6146)/DECODE(H.SLURRY_YIELD,0,NULL,H.SLURRY_YIELD)))*94)*F.ITEM_PERCENT/100 )/P.ATTRIBUTE5)
end)) SUM_sload , ----sload
round(sum(case when (F.ITEM_UOM = 'Cubic Feet' and upper(substr(H.SLURRY_TYPE,1,3)) upper('Top')) then
((H.SLURRY_VOL_ACTUAL * 5.6146)/H.SLURRY_YIELD)
-- when (F.ITEM_UOM = 'Cubic Feet' and upper(substr(H.SLURRY_TYPE,1,3)) = upper('Top')) then
-- nvl(H.CLASS_V_CEMENT,0)
when F.ITEM_UOM = 'Pound' and F.BLEND 'PH'then
ROUND(((ROUND(((H.SLURRY_VOL_ACTUAL * 5.6146)/DECODE(H.SLURRY_YIELD,0,NULL,H.SLURRY_YIELD)))*94)*F.ITEM_PERCENT/100 )/P.ATTRIBUTE5)
end)) BLENDING,
e.opn_job_desc
FROM xxnp_opn_joblog_001 E,
XXNP_OPN_JOBLOG_EST_002 F,
XXNP_OPN_JOBLOG_STAGE_002 G,
XXNP_OPN_JOBLOG_SLURRY_003 H,
XXNP_OPN_JOBLOG_RES_005 J,
MTL_SYSTEM_ITEMS_B P
WHERE E.OPN_JOB_DESC = E.OPN_JOB_DESC AND
E.MANUAL='N'
and J.opn_resource_desc='4X4 PICK-UP OR LIGHT VEHICLES'
AND E.OPN_JOBLOG_001_ID = J.OPN_JOBLOG_001_ID
and F.INVENTORY_ITEM_ID=P.INVENTORY_ITEM_ID
AND p.ORGANIZATION_ID='103'
AND E.OPN_JOBLOG_001_ID = G.OPN_JOBLOG_001_ID
AND G.OPN_JOBLOG_006_ID = H.OPN_JOBLOG_006_ID
AND H.OPN_JOBLOG_007_ID = F.OPN_JOBLOG_007_ID
AND G.OPN_JOBLOG_006_ID = H.OPN_JOBLOG_006_ID
AND H.OPN_JOBLOG_007_ID = F.OPN_JOBLOG_007_ID
GROUP BY E.OPN_JOB_DESC,j.opn_value -
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 ) );� {�
Maybe you are looking for
-
Replication of BP from CRM to R/3
Hi all, I got a problem in moving the business partner form CRM 4.0 to R/3 backend. In doing so, a mBDoc gets generated with the status 'sent to receivers ( not all have confirmed )' and when i see the error detail, it says 'Make an entry in al
-
I am trying to download mountain lion on to a mac pro but the message I get is just an error has occurred. any suggestions are very welcome.
-
Connecting to bb app error message
I have had my bb 1 month and been trying to connect to app world but keep getting an error message saying cannot connect please verify your settings as cannot connect to bb server. Looked everywhere and cannot rectify it. Help please......
-
Unable to update Lightroom 2.0 to 2.2
I am using Mac OS X vs. 10.5.6 and have Lightroom 2.0 installed. I downloaded vs. 2.2 from Adobe and clicked through the installation process until it told me that the installation is complete. The icon on the desktop depicts the Lr in a gray square
-
How can I redirect the URL without telling the user about it?
I am working with blazeDS and the access to it goes thru a specific port that you have to enter like this: http://www.go.com:123 So I wont to redirect my users to this port without showing this in they link bar, so when they press "Refresh" it will s