简体   繁体   English

带有小数点的osclass的价格数字格式

[英]Price number format for osclass with comma decimal point

Hi guys ho do i format this to a number field where you can use only number separated with comma and decimal point (This is for a price field) ex 150,850.00 嗨,大家好,我将其格式化为数字字段,在其中只能使用以逗号和小数点分隔的数字(这是用于价格字段),例如150,850.00

And how to let appears the comma and decimal point auto generated? 以及如何让逗号和小数点自动显示?

<input 
  placeholder="<?php osc_esc_html(_e('Price', 'ctg_housing')) ; ?> 
  <?php osc_esc_html(_e('Min', 'ctg_housing')) ; ?>" 
  type="text" id="priceMin" name="sPriceMin" 
  value="<?php echo osc_esc_html(osc_search_price_min()); ?>" 
  onkeypress="return isNumber(event)" />

Thanks 谢谢

Maybe something like this: 也许是这样的:

$(document).ready(function(){
    $('#priceMin').keyup(function(event){
        var $elm = $(this);
        var value = $elm.val();
        var result = value.replace(/[^0-9\.\,]+/g, "");
        $elm.val(result);
    });
});

Below is the JS code that would add comma to the numbers and also a code that would remove comma from the numbers. 以下是将comma添加到数字的JS代码,以及将从数字中删除逗号的代码。 It's a function that accepts number, so you can use it anywhere: 这是一个接受数字的函数,因此您可以在任何地方使用它:

 function getNumberWithCommas(yourNumber) { var n = yourNumber.toString().split("."); n[0] = n[0].replace(/\\B(?=(\\d{3})+(?!\\d))/g, ","); return n.join("."); } var numberWithComma = getNumberWithCommas(1136324); console.log(numberWithComma); numberWithComma = numberWithComma.replace(/\\,/g, ''); console.log(parseInt(numberWithComma)); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM