te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Using toFixed(2) and math round to get correct rounding - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using toFixed(2) and math round to get correct rounding - Stack Overflow

programmeradmin5浏览0评论

I would like to find a function that will return this kind of formatted values :

1.5555 => 1.55
1.5556 => 1.56
1.5554 => 1.55
1.5651 => 1.56

toFixed() and math round return this value :

1.5651.fixedTo(2) => 1.57

This will be usefull for money rounding.

I would like to find a function that will return this kind of formatted values :

1.5555 => 1.55
1.5556 => 1.56
1.5554 => 1.55
1.5651 => 1.56

toFixed() and math round return this value :

1.5651.fixedTo(2) => 1.57

This will be usefull for money rounding.

Share Improve this question edited Apr 4, 2011 at 8:31 mplungjan 178k28 gold badges181 silver badges240 bronze badges asked May 18, 2010 at 20:47 bdo334bdo334 4082 gold badges9 silver badges20 bronze badges 6
  • 1 You have the fourth decimal affect the second decimal? When ever would you use this? – Matti Virkkunen Commented May 18, 2010 at 20:50
  • 6 Why would you want to round in such a strange way? How would this be useful for money rounding? – sth Commented May 18, 2010 at 20:55
  • 1 Your examples don't really seem to be getting your point across. What's the rule you're trying to enforce, exactly? Round down on a final "5" digit, unless the next digit is > 5? – Mark Bessey Commented May 19, 2010 at 0:22
  • Indeed, this is a strange way of rounding. Doesn't really make any sense to me. – Sasha Chedygov Commented May 19, 2010 at 0:51
  • Isn't that how Richar Pryor made all that money in that Superman movie? – kennebec Commented May 19, 2010 at 1:33
 |  Show 1 more ment

3 Answers 3

Reset to default 6

How about this?

function wacky_round(number, places) {
    var multiplier = Math.pow(10, places+2); // get two extra digits
    var fixed = Math.floor(number*multiplier); // convert to integer
    fixed += 44; // round down on anything less than x.xxx56
    fixed = Math.floor(fixed/100); // chop off last 2 digits
    return fixed/Math.pow(10, places);
}

1.5554 => 1.55

1.5555 => 1.55

1.5556 => 1.56

1.5651 => 1.56

So, that works, but I think you'll find that it's not a generally-accepted way to round. http://en.wikipedia/wiki/Rounding#Tie-breaking

And standard function

fixedTo = function (number, n) {
  var k = Math.pow(10, n);
  return (Math.round(number * k) / k);
}

and then call

fixedTo(1.5555, 2)  // 1.56
fixedTo(1.5555, 2)  // 1.556
fixedTo(0.615, 2)   // 0.62

You can use the native Number.toFixed() method.

parseFloat(10.159.toFixed(2))

The above returns 10.16. Number.toFixed() returns a string, so I use parseFloat to convert it to a number.

发布评论

评论列表(0)

  1. 暂无评论