You need to sign in to do that
Don't have an account?

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?
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?
Try below snippet.
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/
Hope above information was helpful.
Please mark as Best Answer so that it can help others in the future.
Thanks,
All Answers
Try below snippet.
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/
Hope above information was helpful.
Please mark as Best Answer so that it can help others in the future.
Thanks,
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. 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
If this helped you please mark it as the best answer.
Thank you!
Regards
Suraj Tripathi.