通过javascript在文本字段中显示货币格式

currency format in text field through javascript

本文关键字:显示 货币 格式 字段 javascript 文本 通过      更新时间:2023-09-26

我想让文本字段采用货币格式,小数点后2位。问题是:-在货币格式后,我的值保存在数据库中的第一位,而不是完整的值。

这是代码:-

html:-
<?php $itemcost=$item->price;?>
                        <label for="item-price" class="required item-dollar">$</label>
                        <input type="text" class="form-control" required="required" 
                                       value="{{number_format($itemcost)}}" id="item-pricing" 
                                       name="price" placeholder="Item Price">

请告诉我任何解决方案。

不清楚您是否在将货币保存到数据库或显示时遇到困难

您需要将值存储在接受小数/浮点值的字段中(例如十进制(10,2)

如果您使用的number_format()没有第二个默认值为无小数位数的参数,则应该使用number_format($itemcost,2)表示两个小数位数

<?php $itemcost=$item->price;?>
<label for="item-price" class="required item-dollar">$</label>
<input type="text" class="form-control" required="required" value="{{number_format($itemcost,2)}}" id="item-pricing" name="price" placeholder="Item Price">

http://php.net/number_format