tags:

views:

143

answers:

6

hi

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

+10  A: 

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
That truncates to an integer - http://msdn.microsoft.com/en-us/library/c2eabd70.aspx "Truncate rounds d to the nearest integer towards zero."
ChrisF
@ChrisF, indeed; I updated my answer
Thomas Levesque
@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
@Dan - The code was edited after @ChrisF downvoted, check the edits.
TheCloudlessSky
@TheCloudlessSky - just to point out I didn't downvote, just commented. I like to give people chance to correct their posts first.
ChrisF
It's not showing any edit here :-/
Dan Puzey
@Dan, I think that's because I deleted the answer, then edited and undeleted it.
Thomas Levesque
@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
@Paul Hadfield: done
Thomas Levesque
@Thomas: Ok, sorted.
Paul Hadfield
A: 

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
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
@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
A: 

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
A: 

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
+5  A: 
System.Math.Truncate (102.123456789 * factor) / factor; 

In your case factor is 10^6; read more

Vash
+2  A: 
  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