使用某种模式格式化数字

时间:2017-11-06 07:45:11

标签: java android string-formatting

我正在寻找一些典型的实现,如下所示:

我需要我的价格 EditText 字段始终从后面获取值,例如:

最初, EditText 默认值为0,

现在,当我开始输入时,说当我从数字键盘按add_action('wp_loaded', 'some_function'); function some_function(){ $Purchase_Credit_Form_Object = WC_SC_Purchase_Credit::get_instance(); remove_action( 'woocommerce_checkout_after_customer_details', array( $Purchase_Credit_Form_Object, 'gift_certificate_receiver_detail_form' ) ); add_action( 'woocommerce_checkout_before_customer_details', array( $Purchase_Credit_Form_Object, 'gift_certificate_receiver_detail_form' ) ); } 时,

它应该导致打印1

接下来,当我按0.01时,

它应打印为2

接下来,我按0.12

它应打印为3 ... 等等..

最大值不能输入超过1.23 ..

的值

是否有人对格式化技巧有好处?

1 个答案:

答案 0 :(得分:1)

假设您需要精确到小数后的2位数:

public void afterTextChanged(Editable s) {
    // handle empty editText value and zero
  double etValue = Double.valueOf(editText.getText().toString());
  String input = s.toString();
  int len = input.length();
    for(int i=0; i<len; i++){
        etValue *= 10;
        etValue += Double.valueOf("0.0"+input.charAt(i));
    }
  editText.setText(String.valueOf(etValue));
}