Here is the code:
function updateCartSubtotal() {
var subtotal = 0.00;
$('span.item-subtotal-value').each(function () {
subtotal = subtotal + parseFloat($(this).text()); //24.00 for example.
});
console.log(subtotal); // Logs: "144"
$('span.cart-subtotal-value').text(subtotal); //Places: "144" in the .text().
$('span.cart-subtotal').text(subtotal);
}
So what am I doing wrong? Why is this ignoring the two trailing zeroes?
It's adding correctly, just not showing the decimals.
123 and 123.00 are the same. When displaying a float there is no reason to display unnecessary digits.
More important, floats are not specific to currencies - so if decimal digits would be displayed, there would have to be many more.
If you want to display the number with a certain number of digits, use subtotal.toFixed(2). It gives you a string with the correct amount of decimal digits:
>>> 123.00
123
>>> (123.00).toFixed(2)
"123.00"
So your code could look like this:
$('span.cart-subtotal-value, span.cart-subtotal').text(subtotal.toFixed(2));
You might be interested in the number_format function from PHPJS, as this will also include thousand separators that are commonly found in currencies.
Related
I have number without decimal places and I want to convert it to two decimal places (while keeping zeros) and keep its number type.
I have tried it like this:
$scope.invoice_data.form_data.items_shipping_handling = parseFloat(($scope.invoice_data.form_data.items_shipping_handling).toFixed(2));
console.log(typeof $scope.invoice_data.form_data.items_shipping_handling);
But it parseFloat doesn't take into account decimal places if they are zeros.
So if I have 2 I want to convert it to 2.00 //number.
Thank you for your time. I mention that the code is in angular so if it is any angular way of doing it I am open to suggestions.
Details: I cannot simply use toFixed(2) I need the result to have decimal places but to keep its number format, toFixed() converts it to string. I need it to be of number type!
Numbers dont have a "number of decimal places" - they're internally just a series of 1's and 0's. To display a number to a set number of decimal places you can use toFixed
var value = 2;
console.log(value.toFixed(2));
If you're trying to round a number to a set of decimal places one way is to multiply by 100, round it and then divide by 100
var value = 2.123456
var rounded = Math.round(value*100)/100;
console.log(rounded);
variable.toFixed(2)
doc here
List item
I have a number of variable length the looks something like this:
48.4532
I want to convert it to 4 digits before decimal and 2 decimal places, so the number above should be:
0048.45
I also don't want to show decimals unless they are necessary, so:
48
should become:
0048
I was able to get the fixed length, but I couldn't get the decimals to show up only if they were necessary (I don't want to show two 0's at the end).
This is how I got a fixed length:
trackPartLength = ("0000" + trackPartLength.toString()).slice(-4); // Convert to fixed length
How do I add the 2 decimal points only if they are needed?
Edit: I also just realized that if the number does have decimals with the above code, it moves the decimal point over 4 spots causing some other problems, so I'm not sure if my initial approach is a good one. I'm trying to right a fixed length function of variable fixed prefix and decimal length.
This should work:
trackPartLength = ("0000" + trackPartLength.toFixed(2)).slice(-7).replace( /\.00$/, '' );
It uses toFixed to get the two decimal points, zero pads and then removes any trailing .00. Examples:
48.4532 -> 0048.45
48 -> 0048
6.213 -> 0006.21
12345 -> 2345
1234.56789 -> 1234.57
If the number can have more than four digits before the decimal, as in the 12345 example, you may want to make the zero padding and slice conditional, so that you don't remove leading digits from a big number. That could be done like this:
var tmp = trackPartLength.toFixed(2);
trackPartLength = (tmp.length >= 7 ? tmp : ('0000' + tmp).slice(-7)).replace( /\.00$/, '' );
Given a variable
var str = 1;
convert str to output 1.00 as a number and not string.
so the output should be 1.00
and not "1.00"
what javascript operations should I use to do this?.
str.toFixed(2) returns a string and not a number so please..
When you're talking about numbers, there is no difference between 1 and 1.00. So, if it's a number, Javascript will treat them the same. If you want it as 1.00, the only real way to do that is by creating a string of it with something like:
var nnn = 1;
var sss = nnn.toFixed(2));
The presentation of that number (either as a string or direct to output) may be under your control but the number itself is not (other than changing the value of course but, as already mentioned, there is no difference between the values 1, 1.0 or 1e0).
JavaScript only has one kind of number: "number", which is a IEEE-754 Double Precision
See this question to format to 2 decimal places.
I have these strings: "59.50" & "30.00"
What I need to do is convert them to integers but keep the trailing zeros at the end to effectively return:
59.50
30.00
I've tried:
Math.round(59.50 * 1000) / 1000
Math.round(30.00 * 1000) / 1000
but ended up with
59.5
30
I'm assuming I need to use a different method than Math.round as this automatically chops off trailing zeros.
I need to keep these as integers as they need to be multiplied with other integers and keep two decimals points. T thought this would be fairly straight forward but after a lot of searching I can't seem to find a solution to exactly what I need.
Thanks!
Your premise is flawed. If you parse a number, you are converting it to its numerical representation, which by definition doesn't have trailing zeros.
A further flaw is that you seem to think you can multiply two numbers together and keep the same number of decimal places as the original numbers. That barely makes sense.
It sounds like this might be an XY Problem, and what you really want to do is just have two decimal places in your result.
If so, you can use .toFixed() for this:
var num = parseFloat("59.50");
var num2 = parseFloat("12.33");
var num3 = num * num2
console.log(num3.toFixed(2)); // 733.64
Whenever you want to display the value of the variable, use Number.prototype.toFixed(). This function takes one argument: the number of decimal places to keep. It returns a string, so do it right before viewing the value to the user.
console.log((123.4567).toFixed(2)); // logs "123.46" (rounded)
To keep the decimals - multiply the string by 1
example : "33.01" * 1 // equals to 33.01
Seems you are trying to retain the same floating point, so better solution will be some thing like
parseFloat(string).toFixed(string.split('.')[1].length);
If you want numbers with decimal points, you are not talking about integers (which are whole numbers) but floating point numbers.
In Javascript all numbers are represented as floating point numbers.
You don't need the trailing zeros to do calculations. As long as you've got all the significant digits, you're fine.
If you want to output your result with a given number of decimal values, you can use the toFixed method to transform your number into a formatted string:
var num = 1.5
var output = num.toFixed(2) // '1.50'
// the number is rounded
num = 1.234
output = num.toFixed(2) // '1.23'
num = 1.567
output = num.toFixed(2) // '1.57'
Here's a more detailed description of toFixed: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed
Can't seem to find a good answer to this question. How do I remove the last 11 digits from an int?
The id could be one or more numbers in the beginning but there will always be 11 digits following the id. There will always be an id in the beginning. {id}{11 digits}.
var getId = function (digits) {
// Do some stuff and return id
}
getId(110000000001); // Should return 1
getId(1110000000001); // Should return 11
getId(2010000000001); // Should return 20
Divide by 1e11 and take the floor:
var stripped = Math.floor(id / 1e11);
This avoids conversion to/from a string representation.
Note that the nature of JavaScript numerics is such that your "raw" values (the values with the 11 useless digits) can't have more than 5 digits in addition to those 11 before you'll start running into precision problems. I guess if you never care about the low-order 11 digits that might not be a problem.
Try this:
var str = 1100000000011;
var res = str.toString().substr(0, str.toString().length - 11);
Demo
You can convert your number to string and delete tailing digits:
digits.toString().replace(/\d{11}$/, '')
By the way, you better don't use ints (or to be precise, Numbers) because if number is greater than 2147483648 (by absolute value), it'll be represented internally as double resulting in precision loss. If don't need tailing digits, then it's okay — use division approach suggested in other answers (this one could break because of scientific notation). But if you want to keep all the data, you should represent your numbers with strings.
You can use division and 10^11 to do so.
Edit: my bad
var getId = function (digits) {
var id = parseInt((digits/(1e11)),0);
}
You can convert the number to a string and slice the last 11 characters from the end
parseInt( digits.toString().slice(0, -11) );
Using .slice( 0 , [ negative_number ]); will slice x characters from the end of the string
More information on .slice() Here