 ShowAll Questionssorted byDate Posted Michael M

# how to display only first 2 digits after decimal in apex

Hello, if i have a decimal like
decimal dec = 1.234234235345

i ONLY want to display the number with the first 2 digits after the decimal, meaning 1.23. How can I do this in apex? Best Answer chosen by Michael M  Vinay (Salesforce Developers) Hi Michael,

Try below snippet.

```Decimal toround = 3.14159265;
Decimal rounded = toround.setScale(2);
system.debug(rounded);```
Check below references.

https://salesforce.stackexchange.com/questions/11686/decimals-in-apex-setting-scale-without-rounding
https://salesforce.stackexchange.com/questions/957/round-a-decimal-to-two-decimal-places
https://www.forcetalks.com/salesforce-topic/how-to-round-the-double-to-two-decimal-places-in-salesforce-apex/

Please mark as Best Answer so that it can help others in the future.

Thanks,  Vinay (Salesforce Developers) Hi Michael,

Try below snippet.

```Decimal toround = 3.14159265;
Decimal rounded = toround.setScale(2);
system.debug(rounded);```
Check below references.

https://salesforce.stackexchange.com/questions/11686/decimals-in-apex-setting-scale-without-rounding
https://salesforce.stackexchange.com/questions/957/round-a-decimal-to-two-decimal-places
https://www.forcetalks.com/salesforce-topic/how-to-round-the-double-to-two-decimal-places-in-salesforce-apex/

Please mark as Best Answer so that it can help others in the future.

Thanks,
This was selected as the best answer Suraj Tripathi 47
HI Michael,
Greetings!!

The decimal class in the apex has the method called setscale() it is used to set the decimal value to the specific number or places.
Here is the example.
```Decimal myValue= 8.8432108523;
Decimal setScaledValue = myDecimal.setscale(2);
```
the value will now become the '8.84'.
You can also Round-Up the values as per the need.
Here is the link for all the methods of the decimal class.
https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_decimal.htm Michael M