DEV Community

Discussion on: Daily Challenge #49 - Dollars and Cents

Collapse
 
ynndvn profile image
La blatte • Edited

Here we go!

f=a=>`$${a.toFixed`2`}`;

And the results:

f(.4); // "$0.40"
f(0); // "$0.00"
f(6.2); // "$6.20"

And how about some Intl?

f=a=>new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(a)

Which does a bit more, like adding commas:

f(.4); // "$0.40"
f(0); // "$0.00"
f(6.2); // "$6.20"
f(100000000.1234); // "$100,000,000.12"
Collapse
 
hellotusharkhanna profile image
Tushar Khanna • Edited

Hello, I am surprised, you can call a function with ``. How come is it working?

Collapse
 
ynndvn profile image
La blatte

ES6 added this functionality, called Tagged Template! A few examples can be found here: wesbos.com/tagged-template-literals/

Thread Thread
 
hellotusharkhanna profile image
Tushar Khanna

Thanks a ton :) learnt new thing