Divide an integer by 100 and get value till two decimal points? [closed]

StackOverflow https://stackoverflow.com/questions/22631074

  •  20-06-2023
  •  | 
  •  

Вопрос

I have String amount in a TextBox, which I want to divide by 100.

The value should round off to two decimal places. I tried and I am not getting a proper value.

Here is what I tried:

6766/100 = 67.66
47/100 = .47
98/100 = .98
Это было полезно?

Решение

Use Math.Round. This example should get you going

string txtText = "78907";

double inputValue;

if (double.TryParse(txtText, out inputValue))
   double result = Math.Round(inputValue / 100, 2);

Output: 789.07

Другие советы

Use Math.Round, but one of both need to be a decimal type to avoid integer division:

double result = Math.Round(6766d / 100, 2);

Use Math.Round. It has a parameter called precision.

Example:

Math.Round(1.23456, 2) -> 1.23

Math.round will do.

Math.Round(1.23456, 2);

It will round input at 2 decimals

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top