I am trying to convert numbers into comma separated format with two decimal places for each and every number in javascript:
My code:
Number(parseFloat(n).toFixed(2)).toLocaleString('en');
This code doesn't show two decimal places (.00) for whole numbers.
I am expecting following results for a set of numbers:
10000 => 100,00.00
123233.12 => 123,233.12
300000.5 => 300,000.50
Appreciate your answer, thanks.
You can use the minimumFractionDigits option of the toLocaleString function.
// result 3,000.00
Number(parseFloat(3000).toFixed(2)).toLocaleString('en', {
minimumFractionDigits: 2
});
// result 123,233.12
Number(parseFloat(123233.12).toFixed(2)).toLocaleString('en', {
minimumFractionDigits: 2
});
You can even remove the parseFloat,toFixed and Number function usage as well, if this is not used for some logic other than displaying the decimal value up to 2 digits.
Related
I am working with GBP currency in Javascript and converting a string currency to a number like the following: Number('1.20') will give me 1.2. But I want to maintain the 0 when converting to a number type. Is this possible? This is the result that I want : 1.20. Can anyone help me please?
A better way to work with Currency in Javascript is to use the Intl.NumberFormat. For course, the output will be of the type: 'String'
The output will take care of the number of decimal places depending on the Currency you specify. In your case GBP so it will be 2 decimal places.
Example:
const number = 1.2;
console.log(new Intl.NumberFormat('en', { style: 'currency', currency: 'GBP' }).format(number));
You can control the decimals like this in Javascript:
let num = 1.204;
let n = num.toFixed(2)
In a javascript code, I have a requirement to format a decimal number to a specific number of decimal places and get its exact string representation. For example, If the number is 999999999.9 and the number of decimal places is 8, then the expected value should be "999999999.90000000"
When the Number.toFixed(8) is used it returns a rounded value which is not what I want. Please refer the below code
var num = 999999999.9
var string_rep = num.toFixed(8)
>> the value of string_rep is "999999999.89999998"
I used num.toString() and tried to manually format the decimal part by adding/removing digits, but it does not work for very small numbers like "0.00000008" as the function toString() returns
the scientific notation, i.e. something like "9e-8"
So what should be the proper approach for this?
Number.prototype.toLocaleString will do the trick
num.toLocaleString('en-US', {minimumFractionDigits: 8, useGrouping: false})//"999999999.90000000"
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 need to format a number for a project im working on at work, only problem is that I cant format it how i want.
I convert the number to a localestring using the toLocaleString method which gives me the commas but i also need decimal places, nothing i seem to do works.
var number = 123.322
number = parseFloat(number).toFixed(2) //123.22
number.toLocaleString() //123.22
The above code just returns the parsefloated number along with the tofixed decimal values but it doesn't add the commas.
How do i get a number to have two decimal places (when the value is 'xx.00') and also be comma separated. Is this possible in JavaScript?
You can give an object to .toLocaleString() which describes what you want:
var sNumber = (10123.322).toLocaleString(undefined,
{'minimumFractionDigits':2,'maximumFractionDigits':2});
Documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString
Original:
const fNumber = 10123.322;
const sNumber = parseFloat(fNumber.toFixed(2)).toLocaleString();
console.log(sNumber);
The number is already in decimal/float format on the first line.
.toFixed(2) turns it into a string using fixed-point notation.
parseFloat() takes that string and turns it back into a float.
.toLocaleString() turns it into a string using the local format.
Just to do it in one line
var num = '12233.3366554';
num = parseFloat(parseFloat(num).toFixed(2)).toLocaleString('en-IN', { useGrouping: true });
Yes, it is possible using .toLocaleString, yo just need to specify the language, optionally you can specify decimals and currency. look at this example:
35000.2455.toLocaleString('en-IN', {minimumFractionDigits: 2, maximumFractionDigits: 2,style: 'currency', currency: 'USD' })
this returns $35,000.25
Number.toLocaleString works on a number, but toFixed returns a string.
Coerce the string back into a number first
var number = 123.322;
var string = parseFloat(number).toFixed(2);
var parsed = (+string).toLocaleString();
console.log(parsed);
In order to get commas you have to specify the locale .The locale en includes commas for the numbers. toFixed() Returns a string. toLocaleString() function commas works on a number not on a string.So parse the string to float.
var number = 1234567.322;
number = parseFloat(parseFloat(number).toFixed(2)).toFixed(2) ;
number=number.toLocaleString('en');
toLocaleString function provide number representation based on languages
var number = 3500.00;
if(Number.isInteger(number)){
var zeroappend= number.toLocaleString()+".00";
console.log(zeroappend);//3,500.00;
}else{
console.log(number.toLocaleString());
}
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.