做的EditText只显示两位小数 [英] making EditText to show only two decimal places
问题描述
我想只显示小数点后两位在我的编辑文本,OFC我想显示编辑文本货币,但小数点后的值限制为2位。
我已经看到了使用普通的前pression一些解决方案,但我不想这样做。我已被告知,Java支持一些内部库函数可以做到这一点。任何一个可以请给我提示或者给我一些有效的code表示。
问候
amount.setRawInputType(Configuration.KEYBOARD_12KEY); amount.addTextChangedListener(新TextWatcher()
{ @覆盖
公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
// TODO自动生成方法存根
DecimalFormat的formatVal =新DecimalFormat的(## ##);
字符串格式化= formatVal.format(S);
amount.setText(格式化); }
您可以简单地使用<一个href=\"http://developer.android.com/reference/java/text/DecimalFormat.html\"><$c$c>DecimalFormat$c$c>
DecimalFormat的格式=新DecimalFormat的(## ##);
字符串格式化= format.format(22.123);
editText.setText(格式化);
您会导致的EditText
为 22.12
i want to show only two decimal places in my edit text, ofc i wanna show currency in edit text but limiting its value to 2 digits after decimal.
I have seen some solutions using regular expression but i don't wanna do that. I HAVE been told that java support some internal library functions that can do that. Any one can please give me hints or give me some efficient code for that.
Regards
amount.setRawInputType(Configuration.KEYBOARD_12KEY);
amount.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
DecimalFormat formatVal = new DecimalFormat("##.##");
String formatted = formatVal.format(s);
amount.setText(formatted);
}
You can simply use DecimalFormat
DecimalFormat format = new DecimalFormat("##.##");
String formatted = format.format(22.123);
editText.setText(formatted);
You will get result in EditText
as 22.12
这篇关于做的EditText只显示两位小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!