1

how to take 6 numbers after the dot - but without round the number ?

for example:

102.123456789 => 102.123456

9.99887766 => 9.998877

in C# winforms

thak's in advance

Agnel Kurian
  • 57,975
  • 43
  • 146
  • 217
Gold
  • 60,526
  • 100
  • 215
  • 315
  • So you simply want to "chop" the number at the sixth decimal place rather than carrying out *any* form of rounding? – John Parker Aug 12 '10 at 12:19
  • I would suggest [Math.Round](http://msdn.microsoft.com/en-us/library/75ks3aby.aspx), but you state you want truncation not rounding. – ChrisF Aug 12 '10 at 12:22
  • 2
    http://stackoverflow.com/questions/329957/truncate-decimal-number-not-round-off – Iain Ward Aug 12 '10 at 12:28

6 Answers6

10

You can use the Math.Truncate method and a 10^6 multiplier:

decimal x = 102.12345689m;
decimal m = 1000000m;
decimal y = Math.Truncate(m * x) / m;
Console.WriteLine(y); // Prints 102.123456
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • That truncates to an integer - http://msdn.microsoft.com/en-us/library/c2eabd70.aspx "Truncate rounds d to the nearest integer towards zero." – ChrisF Aug 12 '10 at 12:20
  • @Chris: the code works. He's using the 10^6 multiplier to work around that issue - actually fairly elegant, and much cleaner than using strings! – Dan Puzey Aug 12 '10 at 12:27
  • @Dan - The code was edited after @ChrisF downvoted, check the edits. – TheCloudlessSky Aug 12 '10 at 12:28
  • 1
    @TheCloudlessSky - just to point out I didn't downvote, just commented. I like to give people chance to correct their posts first. – ChrisF Aug 12 '10 at 12:36
  • @Dan, I think that's because I deleted the answer, then edited and undeleted it. – Thomas Levesque Aug 12 '10 at 12:47
  • @Thomas, can you just edit your answer then I can remove the down vote. You are correct, I had downvoted the original answer and was in the process of adding a comment when you deleted your original answer. I've just tried to remove down vote and it's telling me it's locked unless you edit the answer. – Paul Hadfield Aug 12 '10 at 13:04
5
System.Math.Truncate (102.123456789 * factor) / factor; 

In your case factor is 10^6; read more

Community
  • 1
  • 1
2
  public decimal TruncateDecimal(decimal decimalToTruncate, uint numberOfDecimalPlacse)
  {
     decimal multiplication_factor = (decimal)Math.Pow(10.0, numberOfDecimalPlacse);
     decimal truncated_value = (long)(multiplication_factor * decimalToTruncate);
     return (truncated_value / multiplication_factor);
  }
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
0

I know this is ugly using strings, but thought I'd put it anyway:

double x = 9.9887766;
string[] xs = x.ToString().Split('.');
double result = double.Parse(xs[0] + "." + xs[1].Substring(0, Math.Min(xs[1].Length, 6)));
TheCloudlessSky
  • 18,608
  • 15
  • 75
  • 116
  • Similar to my solution but you've provided actual code. If you go this route, you may want to check that xs[1] is not null first (calling sub string would through an exception). Also for non null values, what happens if it contains less than 6 characters. – Paul Hadfield Aug 12 '10 at 12:24
  • @Paul - Yeah I caught the 2nd part just before you replied. But yeah... this is a really crude example. If the OP wants to always check if `xs[1]` is null/non-existent then he can do so. – TheCloudlessSky Aug 12 '10 at 12:27
0

Might be a long winded way, but how about turning it into a string, locating the decimal point and then grabbing the string minus anything after the 6th decimal place. You could then turn it back into a decimal.

Paul Hadfield
  • 6,088
  • 2
  • 35
  • 56
0

It's crude but how about:

decimal Number = 102.123456789;
string TruncateTarget = Number.ToString();
decimal FinalValue = Decimal.Parse(TruncateTarget.Substring(0, TruncateTarget.IndexOf('.') +6));
Lazarus
  • 41,906
  • 4
  • 43
  • 54