PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, August 10, 2022

[FIXED] How to add precision to decimal value

 August 10, 2022     c#, decimal, decimal-point     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing