Life is a journey

Who Dares Wins
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Truncate a number to a certain number of decimal places

Posted on 2009-11-05 15:39  zhaobin  阅读(326)  评论(0编辑  收藏  举报

How do you truncate a number to a certain number of decimal places? for example, if i have 1.149 and i want to truncate that to 2 decimal places, the number should be 1.14. math.truncate() changes the number to 1.

Answer:

multiply the number by 10 raised to the power of the number of desired decimal points, use math.truncate() and divide by the original power of 10.

1.149 * (10^2): 114.9
math.truncate(): 114
114 / (10^2): 1.14

ASP.NET Example:

Math.Truncate(Convert.ToDecimal(Eval("AMOUNT")) * 100) /100