3

Possible Duplicate:
c# - How do I round a decimal value to 2 decimal places (for output on a page)

i have value of 1.564 and 1.565 i need round this value to 1.56 and 1.56,which function suitable for this in c#.

Community
  • 1
  • 1
ratty
  • 13,216
  • 29
  • 75
  • 108

2 Answers2

5

do a multiply by 100 followed by a floor and followed by a divide by hundred. I am sure that there is a better way of doing it though

Math.floor(n*100)/100
codebreach
  • 2,155
  • 17
  • 30
2

To remove the less significant digits (1.348 -> 1.34):

Math.Floor(number * 100) / 100;

To round the number to two decimals:

Math.Round(number, 2);

To represent it as a string, for display:

number.ToString("#.00");
Kobi
  • 135,331
  • 41
  • 252
  • 292
  • for this 1.5674 and 1.5675 how can i – ratty Feb 25 '10 at 07:03
  • for this 1.5674 and 1.5675 how can i convert this to 1.567 & 1.567 – ratty Feb 25 '10 at 07:12
  • the same with `1000`, `3` and `#.000`. This assumes you need a **fixed number of decimals**. Do you need a function that removes the last digit? That is weird, isn't it? – Kobi Feb 25 '10 at 07:17