目录
前言
前一段时间做了一个金融类的项目,然后总结了一下对于金钱方面的操作。
设置EditText输入金钱格式(小数点后两位)
布局文件
<!--inputType设置为numberDecimal-->
<EditText
android:hint="请填写最新价格"
android:inputType="numberDecimal"/>
Java代码
//设置过滤器
editText_price.setFilters(new InputFilter[]{new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if(source.equals(".") && dest.toString().length() == 0){
return "0.";
}
if(dest.toString().contains(".")){
int index = dest.toString().indexOf(".");
int length = dest.toString().substring(index).length();
if(length == 3){
return "";
}
}
return null;
}
}});
将数字格式化为金钱格式
DecimalFormat df = new DecimalFormat("0.00");//格式化小数
//DecimalFormat df = new DecimalFormat("#0.00");//与上一行代码的区别是:#表示如果不存在则显示为空,0表示如果没有则该位补0.
//DecimalFormat df = new DecimalFormat("#,###.00"); //将数据转换成以3位逗号隔开的字符串,并保留两位小数
df.setRoundingMode(RoundingMode.FLOOR);//不四舍五入
String money = df.format(v);//返回的是String类型
金钱滚动效果
详情请移步:http://blog.csdn.net/chay_chan/article/details/70196478
github:https://github.com/chaychan/PowerfulViewLibrary