javascript格式价格

Kei*_*wer 4 javascript format

我希望将变量格式化为价格格式,如果它是例如$ 90,则忽略它已经做过的小数.但如果价值是44.5美元,那么我想将其格式化为44.50美元.我可以在php而不是javascript中做到这一点.

PHP示例:

number_format($price, !($price == (int)$price) * 2);
Run Code Online (Sandbox Code Playgroud)

我要格式化的代码:

$(showdiv+' .calc_price span').html(sum_price);
Run Code Online (Sandbox Code Playgroud)

Jam*_*mes 17

var price = 44.5;    
var dplaces = price == parseInt(price, 10) ? 0 : 2;
price = '$' + price.toFixed(dplaces);
Run Code Online (Sandbox Code Playgroud)