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?

Share Improve this question edited May 31, 2012 at 13:00 Esailija 140k23 gold badges279 silver badges328 bronze badges asked May 31, 2012 at 12:58 user892134user892134 3,21419 gold badges67 silver badges139 bronze badges 3
  • 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, do bidnumber % 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
Add a ment  | 

3 Answers 3

Reset to default 9

You 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