0

I want to extract N number of decimal points after the value without doing round up.

Below is the example :

string val = null;
int numberOfDigitsAfterDecimalPoint = 2;
double val1 = 56423747.61;
double val2 = 56423996.57;
val = ((56423747.61 / 56423996.57) * 100).ToString(); //99.9995587692912  
val = String.Format("{0:n" + numberOfDigitsAfterDecimalPoint.ToString() + "}", (100 * Convert.ToDecimal(val)) / 100); //100.00

But problem here is it is rounding up and I am getting 100.00 which I don't want because I want exact value with decimal point i.e 99.99 without any kind of round up.

I searched and came to conclusion(my thinking) that best way to handle this is by extracting number of digits after decimal point with substring method but still I am not sure that whether i am thinking in wrong or right way.

Expected output with numberOfDigitsAfterDecimalPoint = 2 :

99.99

Update

I am not having a fixed value to get after decimal point because it is dependent on numberOfDigitsAfterDecimalPoint variable. Apart from that can have very large value based on which I am calculating val; that is why I was thinking to use substring function in which I won't have any problem related to round off, as oppose to mathematical calculation or math function.

How can I do this in efficient way without compromising any value?

halfer
  • 19,471
  • 17
  • 87
  • 173
  • 1
    You're dealing with base 2 floating point numbers. The exact value in base 10 representation almost always involves rounding. Extracting the values by string manipulation is simple and probably the least compromising. Otherwise, `Math.Floor((decimal) 99.9995587692912 * 100m) / 100m` is *probably* pretty safe, but you'll still get rounding in extreme cases (very large numbers). – Jeroen Mostert Sep 14 '17 at 14:42
  • @JeroenMostert:Yes you are right because in most of the answers i have seen the problem of round off with large numbers too and i will have very large numbers too that is why u was thinking to extract values by string manipulation. – Learning-Overthinker-Confused Sep 14 '17 at 14:50
  • `val = x.ToString("N17", CultureInfo.InvariantCulture); val = val.Substring(0, val.IndexOf('.') + 1 + numberOfDigitsAfterDecimalPoint)` (for `numberOfDigitsAfterDecimalPoint > 0`). This is not necessarily efficient, but it is WYSIWYG. – Jeroen Mostert Sep 14 '17 at 15:04
  • @Learning Even when the truncation is determined by a variable, you can use [this](https://stackoverflow.com/a/10079632/5894241) answer from the question tagged as duplicate. – Nisarg Shah Sep 14 '17 at 15:41
  • @Joroen Mostert Can you write this as an answer with some explanation – Learning-Overthinker-Confused Sep 14 '17 at 15:59
  • A little late here: If you absolutely must know and contain error, consider using fixed point path instead. – Michael Dorgan Sep 14 '17 at 16:37
  • @JeroenMostert What do you suggest whether i should go with string manipulation or with Math.Floor((decimal) 99.9995587692912 * 100m) / 100m? – Learning-Overthinker-Confused Sep 15 '17 at 10:42

0 Answers0