-1

I want to remove the decimals after a price if it ends on ',00'. If it ends on anything else it should remain. I'll have to be able to see on what the price ends to do so, but how do I achieve this in Javascript?

My idea was checking if the price ended on 00 and removing it in an if statement.

function gformFormatMoney(text, isNumeric){ 
if(!gf_global.gf_currency_config)
return text;

var currency = new Currency(gf_global.gf_currency_config);
var unformatted = currency.toMoney(text, isNumeric);
var formatted;
var formatting = unformatted%10;
if(formatting == 00) {

} 

return unformatted;
}

^This gives a error 'Octal litterals with the prefix 0 are not allowed'

Scott van Duin
  • 81
  • 1
  • 12

4 Answers4

1

You should use toFixed.

as for :

let num = 50.00;
num.toFixed(2).includes('.00') ? num.toFixed() :num.toFixed(2);
arielb
  • 392
  • 3
  • 10
1

You need to parse your numbers as a float, fix it to 2 decimals (in all cases), and remove any matches for (.00). Something like this could work:

function fixFloat(num){
  return parseFloat(num).toFixed(2).replace('.00', '');
}

console.log(fixFloat(20.00));
console.log(fixFloat(40.40));
console.log(fixFloat(30.01));

Please be aware that this will return a string. If you wish to convert this back to a number, you'll need to parse it again.

Lewis
  • 3,479
  • 25
  • 40
0

If the data type is not string , the trailing zeros after decimal will be removed. If it is a string use parseInt to convert to number

let price = 20.00;
console.log(price)

let price1 = '40.00'
console.log(parseInt(price1, 10))

let price2 = '40.00'
console.log(parseFloat(price2, 10))
brk
  • 48,835
  • 10
  • 56
  • 78
0

Turns out it wasn't an integer, but a string.

I fixed it by doing:

function gformFormatMoney(text, isNumeric){
if(!gf_global.gf_currency_config)
    return text;

var currency = new Currency(gf_global.gf_currency_config);
var unformatted = currency.toMoney(text, isNumeric);
var formatted = unformatted.replace(',00', '');

return formatted;
}
Scott van Duin
  • 81
  • 1
  • 12