JavaScript格式的价格 [英] javascript format price

查看:177
本文介绍了JavaScript格式的价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将一个变量格式化为一个价格格式,例如,如果是$ 90,那么不管它已经做的十进制数。但是,如果价值是44.5美元,那么我想把它格式化为44.50美元。我可以做到这一点在PHP不JavaScript的。



PHP示例:

$ p $ number_format($ price,!($ price ==(int)$ price)* 2);

我要格式化的代码:

  $(showdiv +'.calc_price span')。html(sum_price); 


解决方案

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


I wish to format a variable to a price format where if it is for example $90 then leave out the decimal which it already does anyway. But if the value is $44.5 then I want to format it as $44.50. I can do it in php not javascript.

PHP example:

number_format($price, !($price == (int)$price) * 2);

The code I want to format:

$(showdiv+' .calc_price span').html(sum_price);

解决方案

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

这篇关于JavaScript格式的价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆