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.
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)
I'm wanting to convert a specific range of numbers to currency. I'm not sure if such formatting exists in google apps script. I notice that when defining one cell value with a "$", it automatically declares any other numbers in that range with a "$". So I assume that there is some kind of currency type.
If so, is there some type of fancy method to convert a number to this currency format or am I to simply convert each number to a string and add a "$" in front of each one.
Any help would be appreciated, thank you.
You can use a built-in Javascript functionality like this:
var numberToCurrency = new Intl.NumberFormat('en-US', {style: 'currency', currency: 'USD'});
numberToCurrency.format(3600);
Google Apps Script uses JavaScript, as the later doesn't have a currency data type, Google Apps Script either. The V8 engine (default for new project) supports Internationalization API, through the Intl object, but the old engine (Rhino) doesn't.
If your project is using the default settings, you might use Intl.NumberFormat (examples taken from MDN)
const number = 123456.789;
console.log(new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(number));
// expected output: "123.456,79 €"
// the Japanese yen doesn't use a minor unit
console.log(new Intl.NumberFormat('ja-JP', { style: 'currency', currency: 'JPY' }).format(number));
// expected output: "¥123,457"
It's worthy to note that Google Sheets stores "currency" values as numbers. The "$" is added by the automatic number formatting feature. If you want that your script adds this formatting use setNumberFormat.
Related
How to format numbers as currency strings
Set cell format with google apps script
Intl.NumberFormat String Currency before number
Is there something i can do here
const ecommerce = {
purchase: {
actionField: {
},
},
}
As it looks like, the inputted value is a string. You could convert the string to number by using an unary plus + in front of the variable.
name: "PRICEPLAN-" + +price,
An other way would include some sanity checks and urges the user to input a valid value.
#Nina Scholz' answer is correct and concise. The other way in JavaScript to do this, and which I personally prefer because it's more semantic, is to use Number().
name: "PRICEPLAN-" + Number(price),
I find that good semantics make it easier for others to understand my code (and for me too, when I come back to it 6 months later.)
As others have pointed out, this will not coerce your values to an integer, so you will want to be sure about your inputs.
If you want Integer value, then you can use var parseInt() function in JS
var a = parseInt("10.00")
will convert it to "10"
radix :
An integer between 2 and 36 that represents the radix (the base in mathematical numeral systems) of the above mentioned string. Specify 10 for the decimal numeral system commonly used by humans. Always specify this parameter to eliminate reader confusion and to guarantee predictable behavior.
After adding radix it will assure the output will be decimal :
var a = parseInt("010.00", 10)
To be sure to have an Interger, use parseInt(). As Number() won't prevent numbers like 3.11 to be transformed to Integers.
name: "PRICEPLAN-" + parseInt(price),
And using ES6 template notation:
name: `PRICEPLAN-${parseInt(price)}`,
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'm attempting to write a currency formatting function using Intl.NumberFormat.
It works correctly when I pass it things like USD, or EUR as the currency, but seems to break when I pass it more obscure currency codes like PLN or COL, and instead of displaying their symbols as requested it displays the Codes. It is clearly recognizing the code because when I ask it to display the name instead it works correctly:
Intl.NumberFormat("en-US",{
style:'currency',
minimumIntegerDigits:1,
currency: 'PLN',
currencyDisplay: 'symbol'
}).format(43);
Displays "PLN43" while
Intl.NumberFormat("en-US",{
style:'currency',
minimumIntegerDigits:1,
currency: 'PLN',
currencyDisplay: 'name'
}).format(43);
Displays "43.00 Polish zlotys"
The Intl.NumberFormat should have the symbols you need, you just have to make sure you specify the correct language code.
You can find a mapping of ISO language codes here:
https://www.w3schools.com/tags/ref_language_codes.asp
In this case you will need to use the Polish value "pl" instead of "en-US"
Intl.NumberFormat("pl",{
style:'currency',
minimumIntegerDigits:1,
currency: 'PLN',
currencyDisplay: 'symbol'
}).format(43);
According to the spec:
However, the set of combinations of currency code and language tag for which localized currency symbols are available is implementation dependent. Where a localized currency symbol is not available, the ISO 4217 currency code is used for formatting.