Display "$xx.90" instead of "$xx.9" in Javascript - javascript

When I use p=10000 ,r=15 and n=60 in the below ...
var x = parseFloat((Math.round(r/12/100*p/(1-Math.pow(1+ +(r/12/100),-n))*100)/100).toFixed(2));
x = 237.9 instead of 237.90.
If the combo of p, r and n result in a number that is not $xx.x", then the code snippet works fine ...ie. formats to 2 decimal places.
But why is it displaying 237.9 instead of 237.90?

When you call number.toFixed(2), you do indeed get a string representation of the number with two decimal digits:
var number = 237.9;
number.toFixed(2); // '237.90'
However, when you then use parseFloat on this, you convert it back to a number again; since a number does not contain information about the number of zeros to display, the last zero is dropped as it is printed:
parseFloat(number.toFixed(2)); // 237.9
To avoid this, simply don't convert your string back into a float, but use it as a string.

var x = parseFloat((Math.round(r/12/100*p/(1-Math.pow(1+ +(r/12/100),-n))*100)/100)).toFixed(2);
p=10000,r=15, n=60;
var x = parseFloat((Math.round(r/12/100*p/(1-Math.pow(1+ +(r/12/100),-n))*100)/100)).toFixed(2);
console.log(x)
Add toFixed after all operations. You need string, basically...

Related

parseInt fixing the value

Here I have a value with commas.
Ex:-var abc = "10,10.12";
If I use var x = parseInt(abc);
it is returning 10
Expected output is 10,10.12
as I have used ng-value="UpdatedPropuesta.VALOR_CUOTA | number:2" in JSP.
If you want an array of numbers out of the string then try this,
const input = "10,10.12";
var output = input.split(",");
output = output.map(i => Number(i));
console.log(output);
10,10.12
That is not the number 1010.12, it is the number 10, a comma operator, and the number 10.12.
Commas are not part of the JavaScript literal syntax.
However, in your case you're passing two arguments to parseInt, the first should be a string to convert (but JS will convert it to a strign) and the second is the radix – the number base – which should be an integer.
So JS's type conversion will lead to:
var x = parseInt('10', 10);
Which is of course 10.
After question update
var x = parseInt("10,10.12");
As comma are not part of JS numeric literals, the parse will stop at the comma because it is not a character that can appear in a number.
So the answer is still 10.

Javascript - Explicit decimal values separated by comma

I have a JavaScript and HTML form to make a different sums and multiplications. In general, the script works fine, but in some cases, it does not apply the decimal separator and does not display decimal values.
This is the JavaScript when it sums the values well calculated in the precedent script with decimal separator (example: 228.8). This script returns the value of the sum 1040+228.8-208 = 1060) but the really result is 1060.8:
function sumResult() {
var bookSumFT = parseInt(document.getElementById("bookSum").value);
var bookRivFT = parseInt(document.getElementById("bookRiv").value)
var bookIVTFT = parseInt(document.getElementById("bookIVT").value)
var bookRitFT = parseInt(document.getElementById("bookRit").value)
document.getElementById("bookAllCalc").value = bookSumFT + bookRivFT + bookIVTFT - bookRitFT;
}
How do I calculate the correct sum with decimal values?
parseInt() will convert your values to integers. That is, without the decimal part.
Use Number() or parseFloat() instead of parseInt()

Javascript format number of decimal places in float

Can anybody help me with this issue i'm having? I'm using the following code to get the value of an input field
parseFloat($("#salaryFrom").val());
The only problem is that if the value in the salaryFrom field ends with a 0 it's getting cut of from the results. e.g. The value 8.50 is being returned as 8.5 and I need it to return 8.50
As a float 8.50 an 8.5 are identical. However when you convert your number to a string you can specify the number of decimal places you would like to use with the function toFixed() e.g.
var a = parseFloat($("#salaryFrom").val());
var b = a.toFixed(2);
The function toFixed() takes the number of decimal places you would like to format your number to, in this case 2.
Javascript has a toFixed() function to format floats. So do try that.
tmp = parseFloat($("#salaryFrom").val());
formatted_val = tmp.toFixed(2);
alert(formatted_val);
Use .toFixed(2) in javascript. (2) - its optional The number of digits after the decimal point. Default is 0 (no digits after the decimal point)
var num = 8.5;
console.log(num.toFixed(2))
Example: Using toFixed
var numObj = 12345.6789;
numObj.toFixed(); // Returns '12346': note rounding, no fractional part
numObj.toFixed(1); // Returns '12345.7': note rounding
numObj.toFixed(6); // Returns '12345.678900': note added zeros
(1.23e+20).toFixed(2); // Returns '123000000000000000000.00'
(1.23e-10).toFixed(2); // Returns '0.00'
2.34.toFixed(1); // Returns '2.3'
-2.34.toFixed(1); // Returns -2.3 (due to operator precedence, negative number literals don't return a string...)
(-2.34).toFixed(1); // Returns '-2.3' (...unless you use parentheses)

Using toFixed(2) in JavaScript is producing undesired results

I'm doing this:
var refundAmount = parseFloat($('#refundAmount2').val().replace('$',''));
var refundReceived = $('#refundReceived');
var remainderAmount = refundAmount-parseFloat(refundReceived.val().replace('$',''));
alert(parseInt(remainderAmount).toFixed(2));
No matter what I do, the result always ends with 2 decimal places being '.00'. So if the first number is 200.12 and the second is 100.08, it should be alerting me with 100.04 but instead I get 100.00.
Why might this be happening?
You used parseInt to convert that number to an integer and then used toFixed(2) to convert it to a number with 2 decimal places. Adding 2 decimal places to an integer will always result in .00.
Try
alert(remainderAmount.toFixed(2));
See DEMO.
You're getting it as an int with parseInt(), then doing the toFixed(). So you're putting decimal places on an int.

Subtraction in JavaScript only returns two digits of the thousands

I have two variables holding integer values:
x = 36,000;
y = 18,045.40;
this is how i subtract:
z = parseInt(x) - parseInt(y);
the result is 15.
If i remove the parseInt the result is 'Nan'.
How do I go about subtracting x with y without rounding off or removing thousands?
many thanks.
Don't put commas in your numbers.
The code you have posted won't even run. I would recommend pulling the ,s out of your numbers and using parseFloat instead. This appears to give the result you want. Demo here:
http://jsfiddle.net/yVWA9/
code:
var x = 36000;
var y = 18045.40;
alert(parseFloat(x) - parseFloat(y));
There is no separator for thousands in Javascript. Your variables are either holding strings and not integer values or you are getting syntax error.
If you have strings and they cannot be changed (like received from service, etc.) then try this:
x = "36,000";
y = "18,045.40";
// remove commas and convert to numbers
function norm(num) { return +num.replace(',', ''); }
console.log(norm(x) - norm(y));

Categories

Resources