0

Is there a way to round a decimal to the nearest decimal place? I need code that will take an unknown number (unknown number of decimal places) and round it to the nearest decimal place.

decimal number = 0.0000029;

// *magic*

// number = 0.000003;
MontanaMan
  • 177
  • 1
  • 1
  • 11
yava43
  • 1
  • 3
  • Maybe [this can help](https://stackoverflow.com/questions/374316/round-a-double-to-x-significant-figures)? – ProgrammingLlama Mar 09 '22 at 05:21
  • Also your code doesn't compile. – ProgrammingLlama Mar 09 '22 at 05:22
  • Are you saying that you have a number with an unknown number of digits past the decimal and you want to apply rounding such that it has "an unknown number of digits - 1" past the decimal? That sounds like a highly unusual requirement and I'd normally expect to find that it's become garbled somewhere along the line from what's actually wanted. – Damien_The_Unbeliever Mar 09 '22 at 07:58
  • 2
    What's the question? `to the nearest decimal place` doesn't mean much - the nearest to what? What you posted isn't the "nearest", it's one less than the current precision. What if the number can't be represented accurately? What if it has 15 more digits that aren't visible when printing with standard precision? – Panagiotis Kanavos Mar 09 '22 at 08:35
  • Sorry, I from Russia and I speak a little English pleasee =( – yava43 Mar 21 '22 at 09:59

1 Answers1

-1

Use the .Round() method:

Decimal number = 0.0000029;        

//Count the number of decimal places:
int placeCount = BitConverter.GetBytes(decimal.GetBits(number)[3])[2];
//Output: 7

//round your decimal to the nearest decimal (one less than the original count)
Decimal number2 = Decimal.Round(number, placeCount - 1); 

Debug.WriteLine(number2.ToString());
//Output: 0.000003
MontanaMan
  • 177
  • 1
  • 1
  • 11
  • 1
    what if the number is not known? =) this is not the answer i need – yava43 Mar 09 '22 at 06:50
  • Ok ... you didn't specify that in your question. I've edited my Answer to show how to count decimal places, then round to the nearest decimal place. If this works for you, please mark it as the accepted answer. – MontanaMan Mar 09 '22 at 07:52
  • It'd be helpful to explain _how_ `placeCount` is determined instead of hand-waving it away as "magic"; that is, where do the indexes `3` and `2` come from and what is their significance? – Lance U. Matthews Mar 09 '22 at 08:08
  • 1
    Sorry ... his original question showed //**Magic** in the code section, so I humorously included that in mine. I'll edit the answer. Thanks for your feedback. – MontanaMan Mar 09 '22 at 08:12
  • Oh, oops, I didn't notice that. I see that's the same solution as [here](https://stackoverflow.com/a/13493771/150605), where there is at least some contention that it's not a reliable solution. – Lance U. Matthews Mar 09 '22 at 08:17
  • Oh ... I guess almost nothing gets 100% consensus, but I've used it for years on widely-downloaded shareware with no problems. [This](https://stackoverflow.com/a/24548881/11699146) explanation for why it's the best solution makes for good reading. – MontanaMan Mar 09 '22 at 08:29