xr = round(x/n) * n

where

x = original floating point number

n = 10^(-[number of digits after decimal])

e.g. x=1.5673454, n = 0.01 (2 digits after decimal point)

xr = 1.57

skip to main |
skip to sidebar
## Tuesday, June 29, 2010

###
Truncating or rounding off a decimal value/array to user-specified number of decimal places

## About Me

## Blog Archive

## Labels

Sometimes, you want to truncate long floating point numbers to keep just the first few digits following the decimal point. The easy way to do this is

xr = round(x/n) * n

where

x = original floating point number

n = 10^(-[number of digits after decimal])

e.g. x=1.5673454, n = 0.01 (2 digits after decimal point)

xr = 1.57

xr = round(x/n) * n

where

x = original floating point number

n = 10^(-[number of digits after decimal])

e.g. x=1.5673454, n = 0.01 (2 digits after decimal point)

xr = 1.57

Subscribe to:
Post Comments (Atom)

## 1 comment:

use round2

rounding PI to 2 decimals:

round2(pi,.01)

Post a Comment