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
Make Discord Bot Send Picture With Message With Nodejs
Scrollable Table With Fixed Headers and Fixed First Column
Javascript Generate Video Thumbnail from Video Url
Javascript Backslash (\) in Variables Is Causing an Error
How to Format Numbers by Prepending 0 to Single-Digit Numbers
How to Sort Elements in an Array into Order and Even Numbers and They Should Be in Ascending Order
Add Hyphen After Every Fourth Character in a String Seperated by Comma
How to Find If a Text Contains Url String
Increase Counter Value Upon Button Click
How to Disable Eslint Rule Max Line Length for Paragraph in <Template> of Vue.Js
How to Count Unique Value from Object of Array in JavaScript
How to Iterate Through Json Nested Objects With Ngfor
Jest Encountered an Unexpected Token: Syntaxerror: Unexpected Token {
Javascript - Remove Milliseconds from Date Object
Prevent Form Submission on Enter Key Press
Mongodb Get Data to Display in HTML
Change Specific Button Text on Click Inside Ngfor
Count Total Number of Checkboxes What Are Checked and Show on Page Via Js