admin管理员组文章数量:1290952
How do i split decimal numbers? The variable bidnumber
is 10.70
.
var bidnumber = $(this).parent('div').siblings('.advert-details').find('tr:eq(3)').find('.advert-details-col-2').attr('data-bid');
bidnumber.toString().split('.');
var first = bidnumber[0];
var second = bidnumber[1];
alert(bidnumber[0]);
second.substr(0, 1);
var finalnumber = first + '.' + second;
When i alert bid[0]
, it should alert 10
.. Instead 1
is alerted. I don't get where i've gone wrong?
How do i split decimal numbers? The variable bidnumber
is 10.70
.
var bidnumber = $(this).parent('div').siblings('.advert-details').find('tr:eq(3)').find('.advert-details-col-2').attr('data-bid');
bidnumber.toString().split('.');
var first = bidnumber[0];
var second = bidnumber[1];
alert(bidnumber[0]);
second.substr(0, 1);
var finalnumber = first + '.' + second;
When i alert bid[0]
, it should alert 10
.. Instead 1
is alerted. I don't get where i've gone wrong?
- split works just fine - jsfiddle/dLPj3 – Shawn Chin Commented May 31, 2012 at 13:01
- You have to store the result of the split method, it isn't in place. – Hunter McMillen Commented May 31, 2012 at 13:02
-
If you want the integer part, just do
Math.floor(bidnumber)
. If you want the fraction, dobidnumber % 1
. If you actually want both in an array, it's easier to do[Math.floor(bidnumber), bidnumber % 1]
. – Darth Egregious Commented May 31, 2012 at 13:02
3 Answers
Reset to default 9You forgot to return the array back from split
function:
bidnumber = bidnumber.toString().split('.');
- The attribute value is already a string, so you don't have to convert it to a string.
- The
split
method doesn't put the array back in the variable. - The
substr
method doesn't put the new string back in the variable.
So:
bidnumber = bidnumber.split('.');
var first = bidnumber[0];
var second = bidnumber[1];
second = second.substr(0, 1);
var finalnumber = first + '.' + second;
Or just:
bidnumber = bidnumber.split('.');
bidnumber[1] = bidnumber[1].substr(0, 1);
var finalnumber = bidnumber.join('.');
Consider also to parse the string to a number and round it:
var finalnumber = Math.round(parseFloat(bidnumber) * 10) / 10;
You should not use split
to break apart the integer and fractional parts of a number.
For example, 10.70
when split (and converting the 70 to cents) would give a different answer to 10.7
even though they're the same number.
var bidnumber = 10.70; // ignoring your DOM query for now
var whole = ~~bidnumber; // quick and nasty 'truncate' operator
var cents = 100 * Math.abs(bidnumber - whole);
The final line ensures that the number of cents is positive even if the original number was negative.
The ~~
operator is actually two instances of the ~
operator, which is a bitwise "not" operator that truncates any number to a 32 bit signed value, throwing away any fractional part. Flipping twice gives the original number, but without the fraction.
That's the easiest way to get a "round towards zero" result, as Math.floor()
would actually give -11
for an input of -10.70
本文标签: javascriptsplit not workingStack Overflow
版权声明:本文标题:javascript - split not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741499900a2382008.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论