How to Stop Parsefloat() from Stripping Zeroes to Right of Decimal

How do I stop parseFloat() from stripping zeroes to right of decimal

simple:

function decimalPlaces(float, length) {
ret = "";
str = float.toString();
array = str.split(".");
if (array.length == 2) {
ret += array[0] + ".";
for (i = 0; i < length; i++) {
if (i >= array[1].length) ret += '0';
else ret += array[1][i];
}
} else if (array.length == 1) {
ret += array[0] + ".";
for (i = 0; i < length; i++) {
ret += '0'
}
}

return ret;
}
console.log(decimalPlaces(3.123, 6));

How do I stop parseFloat from stripping zeros from decimal

You can use Math.floor(100 * num)/100† with toLocaleString.

// converts a string to a float rounded down to the nearest hundredth
const toFloatHundredth = str => Math.floor(100 * parseFloat(str)) / 100;

const oldValue = toFloatHundredth("2.009876");
const addValue = toFloatHundredth("0.506789");

const resultNum = Math.floor(100 * (oldValue+addValue)) / 100;
const resultStr = resultNum.toLocaleString('en-US',
{
style: 'decimal',
minimumFractionDigits: 2,
});

console.log(typeof resultNum + ": " + resultNum); // 2.5 number
console.log(typeof resultStr + ": " + resultStr); // 2.50 string

ParseFloat a string and retain zeroes in decimal part

Just use simple function, and its working well:

function get_number($str) {
$number = '';

$allowed = array('.');

for ($i = 0; $i <= 9; $i++) {
$allowed[] = (string)$i;
}

for ($i = 0; $i < strlen($str); $i++) {
if (in_array($str{$i}, $allowed)) {
$number .= $str{$i};
}
else {
break;
}
}

return $number;
}

parseFloat not working with zero after decimal

Native JSON doesn't provide a way to specify floats precision, you can hack around this by manipulating encoded json string, like

    data = {        something: 18    };        json = JSON.stringify(data, function(key, val) {        if(typeof(val) === 'number')            return '<<<' + val.toFixed(3) + '>>>';        return val;    }).replace(/"<<<|>>>"/g, '');
document.write(json)

how to get a float using parseFloat(0.00)

You can use the string tmp variable and then when you need to add to it use:

tmp = (+tmp + 8).toFixed(2);

JSFIDDLE DEMO

Or simply write a function to do that seeing that you'll have to do that many times:

function strAdd( tmp, num ) {
return (+tmp + num).toFixed(2);
}

Javascript parse float is ignoring the decimals after my comma

This is "By Design". The parseFloat function will only consider the parts of the string up until in reaches a non +, -, number, exponent or decimal point. Once it sees the comma it stops looking and only considers the "75" portion.

  • https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseFloat

To fix this convert the commas to decimal points.

var fullcost = parseFloat($("#fullcost").text().replace(',', '.'));

drop trailing zeros from decimal

There's probably a better way of doing this, but you could use .rstrip('0').rstrip('.') to achieve the result that you want.

Using your numbers as an example:

>>> s = str(Decimal('2.5') * 10)
>>> print s.rstrip('0').rstrip('.') if '.' in s else s
25
>>> s = str(Decimal('2.5678') * 1000)
>>> print s.rstrip('0').rstrip('.') if '.' in s else s
2567.8

And here's the fix for the problem that gerrit pointed out in the comments:

>>> s = str(Decimal('1500'))
>>> print s.rstrip('0').rstrip('.') if '.' in s else s
1500


Related Topics



Leave a reply



Submit