1

So i want 12.6666667 to show up as 12.6 and not 13 or 12.67 This is what i have done

Math.Round((5 * (53 - 32)) / 9, 1)
Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184
user253752
  • 11
  • 1
  • 3
  • What actually is the question? Do you want to have some code? – Marcel Jan 19 '10 at 05:57
  • 2
    It sounds like your question isn't so much "how do I round numbers", but rather, "how do I format numbers so that I always have one digit after the decimal". Is that right? – John Feminella Jan 19 '10 at 05:59
  • Are you attempting just to truncate some digits? Do you need the result to still be a numeric type or is a string representation suitable? – Eric Mickelsen Jan 19 '10 at 06:21

5 Answers5

3

Math.Truncate (x * 10.0) / 10.0 is one way to do it.

Using your numbers:

double x = 5.0 * (53.0 - 32.0) / 9.0;
double result = Math.Truncate (x * 10.0) / 10.0;

Note the use of ".0" at the end of all numbers, this ensures that floating-point math, and not integer math, is used when combining them. Integer math will remove any remainders when dividing numbers, while floating point will keep the stuff to the right of the decimal point, which is what you want.

Justin Grant
  • 44,807
  • 15
  • 124
  • 208
  • Sorry, but I'm downvoting this; there is no reason to do it. His problem is something else; the Round API's are suitable for rounding, or perhaps he's interested in formatting (as observed by the comment from John). – Noon Silk Jan 19 '10 at 06:02
  • @silky, round will round *up*, that's not what the OP wants. They specifically requested 12.666666 be turned into 12.6, not 12.7 as round would do. Scaling and truncating is the best option unless Truncate also provides a fractional variant as well (I'm pretty sure it doesn't). – paxdiablo Jan 19 '10 at 06:06
  • My assumption from reading the OP's question was that he was looking for a truncation solution (not rounding behavior) but was looking to truncate after the first decimal point. This makes it more than a formatting problem since (AFAIK) any formatting API in the .NET framework will round, not truncate, floating point numbers. In any case, let's let the OP weigh in-- only he can solve the mystery. :-) – Justin Grant Jan 19 '10 at 06:10
  • 1
    @unknown, make sure all your variables and constants are floating point, not integral. You should definitely get 11.6 if that's the case (and 53f is definitely 11.6c). – paxdiablo Jan 19 '10 at 06:19
1

If you want formatting (as John has suggested in the comment):

String.Format("{0:0.0}", 12.6666666); // Note: this shows up as 12.7
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
1
d -= d % 0.1; //Where 0.1 is the desired least significant unit.

It should truncate rather than round at whatever precision you would like, and it seems to work fine on negative numbers as well, always rounding towards zero.

Eric Mickelsen
  • 10,309
  • 2
  • 30
  • 41
0

If you want 12.6666667 to show up as 12.6, you'll have to truncate, not round; otherwise you'd get 12.7 to one decimal place.

Personally I'd do that by going ((int) (12.6666667 * 10)) / 10.

Craig Walker
  • 49,871
  • 54
  • 152
  • 212
0

See Decimal formatting without rounding .net

Community
  • 1
  • 1
John K
  • 28,441
  • 31
  • 139
  • 229