__Scenario:__

to convert a number 1.567 to 1.560

so basically it's to replace the last digit in the fraction with a 0.

__Solution:__

1. moves the decimal point by multiplying 100 to the value.

double val = 1.567 * 100 [becomes 156.7]

2. get the whole part of the value by truncating it

double w = Math.Truncate(val); [becomes 156.0]

3. minus off the

**val**with the truncated value

**w**.

double left = val - w; [becomes 0.7]

4. move the

**left**value by dividing it with 100

left = left / 100; [becomes 0.007]

5. deduct the orignal value with the

**left**

double answer = 1.567 - left; [becomes 1.560]

## 1 comment:

Hi, wouldn't:

double val = Math.Truncate(1.567*100)/100

Work?

Post a Comment