Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
ShowAll Questionssorted byDate Posted
VisualForce

# Round the double value

Hi..

I have displayed one double value in PDF visualforce page..

I have to round that number into two decimal place values..

I am using getter method for getting that value for PDF...

In my controller I am made some calculation in the getter method..

my code

public double getpay()
{
if(tot!=0)
{
pay=cost/tot;
}
return (Math.round(pay*100)/100);
}

Here  tot,pay,cost all are double value..

In my PDF I am getting 1.0 instead of 1.25..

How to achive this in controller..
If I am using without math.round function, I am getting 1.2345352342698 like that..

Any one help me....

Ron Hess
In the docs, there is detail on dividing with rounding

look for Decimal method divide() :

divide
Decimal divisor, Integer scale, Object roundingMode Decimal

Divides this Decimal by divisor, sets the scale, that is, the number of decimal places, of the result using scale, and if necessary, rounds the value using roundingMode. For more information about the valid values for roundingMode, see Rounding Mode. For example:

Decimal myDecimal = 12.4567;
Decimal divDec = myDecimal.divide
(7, 2, System.RoundingMode.UP);
system.assertEquals(divDec, 1.78);

Ron Hess
In the docs, there is detail on dividing with rounding

look for Decimal method divide() :

divide
Decimal divisor, Integer scale, Object roundingMode Decimal

Divides this Decimal by divisor, sets the scale, that is, the number of decimal places, of the result using scale, and if necessary, rounds the value using roundingMode. For more information about the valid values for roundingMode, see Rounding Mode. For example:

Decimal myDecimal = 12.4567;
Decimal divDec = myDecimal.divide
(7, 2, System.RoundingMode.UP);
system.assertEquals(divDec, 1.78);
This was selected as the best answer
VisualForce

Its work fine..

My working code

decimal pay=0;

public decimal getpay()
{
if(tot!=0)
{
pay=cost/tot;
}
return (pay.divide
(1, 2, System.RoundingMode.UP);

}