How to convert decimal points?

Possible duplicate:
C # - How to round a decimal value to two decimal places (for output on a page)

i has a value of 1.564 and 1.565, I need to round this value to 1.56 and 1.56, which is suitable for this in C #.

+3
source share
2 answers

To delete less significant digits (1.348 → 1.34):

Math.Floor(number * 100) / 100;

To round a number to two decimal places:

Math.Round(number, 2);

To represent it as a string, to display:

number.ToString("#.00");
+2
source

multiply by 100, followed by the floor, and then split by a hundred. I'm sure there is a better way to do this, though

Math.floor(n*100)/100
+5
source

Source: https://habr.com/ru/post/1734372/


All Articles