Wednesday, August 10, 2022

[FIXED] How to add precision to decimal value

Issue

I want to add precision to the decimal value. For example, I have this value:

decimal number = 10;

I want to make it 10.00. I don't want to convert it to string like number.ToString("#.00")

Currently, I have this method:

decimal CalculatePrecision(decimal value, int precision)
{
    var storedCalculated = decimal.Divide(1, Convert.ToDecimal(Math.Pow(10, precision)));
    return value + storedCalculated - storedCalculated;
}

Is there any good solution for this?


Solution

You can't. 10 and 10.00 are the same number. Only the "presentation" is different. Both "presentations" are strings. The actual number look different. If you need to change the presentation, convert to string.



Answered By - EricSchaefer
Answer Checked By - Terry (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.