ITKeyword,专注技术干货聚合推荐

注册 | 登录

memcahce添加服务器,实现集群

json_ligege 2016-08-05

相关推荐:memcache集群服务:memagent配置使用

本文来自:http://zhumeng8337797.blog.163.com/blog/static/10076891420113431424757/ magent是一款开源的memcached代理服务器软件 地址: http://code.google.

up vote 169 down vote favorite 11

How can I round down a number in JavaScript?

math.round() doesn't work because it rounds it to the nearest decimal.

I'm not sure if there is a better way of doing it other than breaking it apart at the decimal point at keeping the first bit. There must be...

javascript math rounding share | improve this question edited Apr 26 '12 at 10:36 stema 52k 10 65 94 asked Sep 16 '09 at 23:06 Ben Shelock 6,527 21 69 114 18   Round towards zero or towards negative infinity? –  Daniel Brückner Sep 16 '09 at 23:09 add a comment  | 

8 Answers 8

active oldest votes up vote 326 down vote accepted
Math.floor()

is the answer.

share | improve this answer edited Sep 16 '09 at 23:19 roman m 14.7k 23 90 126 answered Sep 16 '09 at 23:08 phoebus 10.4k 2 24 32 2   It's also the slowest method; if you need to perform a lot of these, use the bitwise | operator (see my post). –  geraldalewis Sep 16 '09 at 23:18 6   The | operator won't work on numbers larger than 2147483647. –  Robert L Sep 16 '09 at 23:47 10   The | operator also rounds towards zero, not negative infinity. –  Mike Godin Mar 3 '14 at 21:58 add a comment  |  up vote 51 down vote

Round towards negative infinity - Math.floor()

+3.5 => +3.0
-3.5 => -4.0

Round towards zero - usually called Truncate(), but not supported by JavaScript - can be emulated by using Math.ceil() for negative numbers and Math.floor() for positive numbers.

+3.5 => +3.0 using Math.floor()
-3.5 => -3.0 using Math.ceil()
share | improve this answer edited Jan 25 '13 at 16:15 answered Sep 16 '09 at 23:16 Daniel Brückner 45.8k 9 72 123      Thank you for the completeness but the capitalization is wrong ... and in java-script that makes a HUGE difference. Otherwise I would have upvoted here. –  George Apr 19 '12 at 3:50      I have updated the answer so that the capitalization is now correct. –  chasen Jun 8 '12 at 18:02 14   @George HUGE or huge? :D –  m93a Oct 24 '13 at 15:16 1   You can get the same effect as round-to-zero via x | 0. –  Ahmed Fasih May 6 '16 at 2:59 add a comment  |  up vote 15 down vote

Math.floor() will work, but it's very slow compared to using a bitwise OR operation:

var rounded = 34.923 | 0;
alert( rounded );
//alerts "34"

EDIT Math.floor() is not slower than using the | operator. Thanks to Jason S for checking my work.

Here's the code I used to test:

var a = [];
var time = new Date().getTime();
for( i = 0; i < 100000; i++ ) {
    //a.push( Math.random() * 100000  | 0 );
    a.push( Math.floor( Math.random() * 100000 ) );
}
var elapsed = new Date().getTime() - time;
alert( "elapsed time: " + elapsed );
share | improve this answer edited Oct 2 '09 at 2:31 answered Sep 16 '09 at 23:15 geraldalewis 532 1 3 9 7   ??? I just ran jsdb (www.jsdb.org) which uses Spidermonkey 1.7, and ran a loop to sum up the floor'ed value of x[i] on an array of 100000 floating point numbers, first with Math.floor(), then with bitwise or as you suggest. It took approx the same time, 125 msec. –  Jason S Sep 18 '09 at 14:49 2   Just repeated the test with 500000 floating point numbers, it took approx the same time, approx 625 msec. –  Jason S Sep 18 '09 at 14:51 3   So I don't see how 1.25usec is very slow. –  Jason S Sep 18 '09 at 14:53 3   Can't argue with your data :) I think I may be have confused JS's implementation with ActionScript's (built on EcmaScript; obviously implementation differs). Thanks for checking my work! –  geraldalewis Sep 27 '09 at 3:45 11   They don't do the same thing, either. | converts to a 32-bit integer, truncating; Math.floor rounds down. jsfiddle.net/minitech/UVG2w –  Ryan♦ Sep 19 '12 at 14:16  |  show 2 more comments up vote 10 down vote

You can try to use this function if you need to round down to a specific number of decimal places

function roundDown(number, decimals) {
    decimals = decimals || 0;
    return ( Math.floor( number * Math.pow(10, decimals) ) / Math.pow(10, decimals) );
}

examples

alert(roundDown(999.999999)); // 999
alert(roundDown(999.999999, 3)); // 999.999
alert(roundDown(999.999999, -1)); // 990
share | improve this answer edited Dec 27 '14 at 15:40 answered Feb 13 '14 at 16:47 Petr Hurtak 1,185 10 13      I think a one-liner like this doesn't require a function. –  Hubert Grzeskowiak Apr 9 '15 at 14:19      roundDown(4.56, 2) gives you 4.55, so I don't think it's a good solution. –  cryss Oct 31 '16 at 9:14 add a comment  |  up vote 6 down vote

To round down towards negative infinity, use:

相关推荐:服务器集群系统实现方案详解

一、集群的基本概念       有一种常见的方法可以大幅提高服务器的安全性,这就是集群。     Cluster集群技术可如下定义:一组相互独立的服务器在网络中表现为单

rounded=Math.floor(number);

To round down towards zero (if the number can round to a 32-bit integer between -2147483648 and 2147483647), use:

rounded=number|0;

To round down towards zero (for any number), use:

if(number>0)rounded=Math.floor(number);else rounded=Math.ceil(number);
share | improve this answer edited Aug 2 '16 at 18:31 answered Mar 3 '14 at 21:57 Mike Godin 876 1 7 21 add a comment  |  up vote 4 down vote

Rounding a number towards 0 can be done by subtracting its signed fractional part number % 1:

rounded = number - number % 1;

Like Math.floor (rounds towards -Infinity) this method is perfectly accurate.

There are differences in the handling of -0, +Infinity and -Infinity though:

Math.floor(-0) => -0
-0 - -0 % 1    => +0

Math.floor(Infinity)    => Infinity
Infinity - Infinity % 1 => NaN

Math.floor(-Infinity)     => -Infinity
-Infinity - -Infinity % 1 => NaN
share | improve this answer edited Oct 29 '14 at 23:22 answered Oct 29 '14 at 23:08 Robert 1,788 14 17 add a comment  |  up vote 2 down vote
Math.floor(1+7/8)
share | improve this answer edited Sep 16 '09 at 23:20 roman m 14.7k 23 90 126 answered Sep 16 '09 at 23:14 DigitalRoss 109k 12 186 268      1+7/8 = 1 - Not much need for Math.floor() there :) –  Jason Berry Sep 16 '09 at 23:16 14   Actually it's (7/8)+1 which is not 1. Thank you 3rd grade algebra –  Joe Phillips Sep 16 '09 at 23:24 1   Umm, please actually try this in a javascript program. I did. Display (1 + 7/8) and you will see 1.875. Math.round(...) is 2, Math.floor(...) is 1. What are you guys talking about? –  DigitalRoss Sep 16 '09 at 23:48 1   Or open the Firefox Error Console. Or Firebug. It isn't hard to try. I tried it. 1 + 7/8 is 1.875 in js. Did you possibly forget that all math in js is in floating point? –  DigitalRoss Sep 16 '09 at 23:53 3   It's probably easy to forget that javascript does everything in floating point. In many other languages 1+7/8 is 1, but in js it really is 1.875. –  DigitalRoss Sep 17 '09 at 1:05  |  show 2 more comments up vote 2 down vote

Was fiddling round with someone elses code today and found the following which seems rounds down as well:

var dec = 12.3453465,
int = dec >> 0; // returns 12

For more info on the Sign-propagating right shift(>>) see MDN Bitwise Operators

It took me a while to work out what this was doing :D

But as highlighted above, Math.floor() works and looks more readable in my opinion.

share | improve this answer answered Mar 5 '12 at 14:38 stikoo 122 2 8 1   It also silently kills your number if it doesn't fit in 32 bits. Chromium console: 99999999999999999999999|0 => -167772160 –  Matthias Urlichs Dec 13 '15 at 18:14 add a comment  | 

Your Answer

  draft saved draft discarded

Sign up or log in

Sign up using Google

Sign up using Facebook

Sign up using Email and Password

Post as a guest

Name Email

Post as a guest

Name Email discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged javascript math rounding or ask your own question.

相关推荐:memcache集群服务:memagent配置使用 centos

magent是一款开源的memcached代理服务器软件 地址: http://code.google.com/p/memagent/ 安装magent到/usr/local/下 cd /usr/local mkdir magent cd magent/ wg

Memcache的资料我就不详述了,如果不知道,可以看:memcache笔记想要实现memcache的集群,当然前提就是编译环境安装好,memcache安装好,这就不说了。下面就直接进入主题:代码及注释:

相关阅读排行


用户评论

游客

相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。