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)
Related
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 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());
}
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.
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'm using a quite simple script to format numbers:
gst = '123.45';
currency = 'EUR';
money = gst.toLocaleString('en-US', {
style: 'currency',
currency: currency,
currencyDisplay: 'code'
});
this will out put this:
EUR123.45
which is, what I want. But I find it kind of ugly that there is no space between the currency and the value. Is there a way to manage this without using a .replace() after the operation?
Thanks in advance
I'm positive there is no specification if toLocaleString that will do this without the replace. You either need to prepend or adjust after the toLocaleString operation.