javascript - Price String spliting into amount and currency -
i need splitting string in javascript.
i have string currency. string depends on location.
for example: "1,99€" or "$1,99"
i split string , extract "amount" , "currency" out of it.
if fails, return empty string or null.
does know how solve that?
if expect inputs include both decimals of cent value (ie, comma followed 2 digits) use this:
const amount = money.match(/\d/g).join('') / 100; const curren = money.match(/[^\d,]/g).join('');
javascripts hated implicit type coercion allows divide string numerator number denominator , end number.
to currency, extract non- digit or comma characters , join them.
if can't rely on input including cent value (ie, might receive whole dollar amount without comma or cent digits) try this:
const amount = money.match(/d/g).join('') / (money.includes(',') ? 100 : 1);
Comments
Post a Comment