android 字体像素转换工具类_android工具类,转换大小写,保留小数点处理方法
import java.text.DecimalFormat;
import java.util.Scanner;
/**
* 金額轉(zhuǎn)換
*
* @author Administrator
*/
public class ConvertMoney {
// 大寫數(shù)字
private final static String[] STR_NUMBER = {"零", "壹", "貳", "叁", "肆", "伍",
"陸", "柒", "捌", "玖"};
// 整數(shù)單位
private final static String[] STR_UNIT = {"", "拾", "佰", "仟", "萬", "拾", "佰",
"仟", "億", "拾", "佰", "仟"};
// 小數(shù)單位
private final static String[] STR_UNIT2 = {"厘", "分", "角"};
/**
* 程序入口
* @param args
*/
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner scan = new Scanner(System.in);
System.out.println("請輸入金額:");
// 獲取金額轉(zhuǎn)換后的字符串
String convert = ConvertMoney.convert(scan.nextDouble());
// 輸出轉(zhuǎn)換結(jié)果
System.out.println(convert);
}
/**
* 獲取整數(shù)部分
*
* @param num
* 金額
* @return 金額整數(shù)部分
*/
public static String getInteger(String num) {
// 判斷是否包含小數(shù)點(diǎn)
if (num.indexOf(".") != -1) {
num = num.substring(0, num.indexOf("."));
}
// 反轉(zhuǎn)字符串
num = new StringBuffer(num).reverse().toString();
// 創(chuàng)建一個StringBuffer對象
StringBuffer temp = new StringBuffer();
for (int i = 0; i< num.length(); i++) {// 加入單位
temp.append(STR_UNIT[i]);
temp.append(STR_NUMBER[num.charAt(i) - 48]);
/*
* num.charAt(i)-48獲取數(shù)值或者使用Integer.pa……
* ASCLL表中0的位置是48,比如得到的字符3,對應(yīng)ASCLL值 是51,減去48才是這里要的值
*/
}
// 反轉(zhuǎn)字符串
num = temp.reverse().toString();
// 替換字符串的字符
num = numReplace(num, "零拾", "零");
num = numReplace(num, "零佰", "零");
num = numReplace(num, "零仟", "零");
num = numReplace(num, "零萬", "萬");
num = numReplace(num, "零億", "億");
num = numReplace(num, "零零", "零");
num = numReplace(num, "億萬", "億");
// 如果字符串以零結(jié)尾將其除去
if (num.lastIndexOf("零") == num.length() - 1) {
num = num.substring(0, num.length() - 1);
}
return num;
}
/**
* 獲取小數(shù)部分
*
* @param num
* 金額
* @return 金額的小數(shù)部分
*/
public static String getDecimal(String num) {
// 判斷是否包含小數(shù)點(diǎn)
if (num.indexOf(".") == -1) {
return "";
}
num = num.substring(num.indexOf(".") + 1);
// 反轉(zhuǎn)字符串
num = new StringBuffer(num).reverse().toString();
// 創(chuàng)建一個StringBuffer對象
StringBuffer temp = new StringBuffer();
// 加入單位
for (int i = 0; i < num.length(); i++) {
if (num.length() == 1) {
temp.append(STR_UNIT2[i + 2]);
temp.append(STR_NUMBER[num.charAt(i) - 48]);
} else if (num.length() == 2) {
temp.append(STR_UNIT2[i + 1]);
temp.append(STR_NUMBER[num.charAt(i) - 48]);
} else if (num.length() >= 3) {
temp.append(STR_UNIT2[i]);
temp.append(STR_NUMBER[num.charAt(i) - 48]);
}
}
// 反轉(zhuǎn)字符串
num = temp.reverse().toString();
// 替換字符串的字符
num = numReplace(num, "零角", "零");
num = numReplace(num, "零分", "零");
num = numReplace(num, "零厘", "零");
num = numReplace(num, "零零", "零");
// 如果字符串以零結(jié)尾將其除去
if (num.lastIndexOf("零") == num.length() - 1) {
num = num.substring(0, num.length() - 1);
}
return num;
}
/**
* 替換字符串中內(nèi)容
*
* @param num
* 字符串
* @param oldStr
* 被替換內(nèi)容
* @param newStr
* 新內(nèi)容
* @return 替換后的字符串
*/
public static String numReplace(String num, String oldStr, String newStr) {
while (true) {
// 判斷字符串中是否包含指定字符
if (num.indexOf(oldStr) == -1) {
break;
}
// 替換字符串
num = num.replaceAll(oldStr, newStr);
}
// 返回替換后的字符串
return num;
}
/**
* 金額轉(zhuǎn)換
*
* @param d
* 金額
* @return 轉(zhuǎn)換成大寫的全額
*/
public static String convert(double d) {
// 實(shí)例化DecimalFormat對象
DecimalFormat df = new DecimalFormat("#0.###");
// 格式化double數(shù)字
String strNum = df.format(d);
// 判斷是否包含小數(shù)點(diǎn)
if (strNum.indexOf(".") != -1) {
String num = strNum.substring(0, strNum.indexOf("."));
// 整數(shù)部分大于12不能轉(zhuǎn)換
if (num.length()> 12) {
System.out.println("數(shù)字太大,不能完成轉(zhuǎn)換!");
return "";
}
}
String point = "";// 小數(shù)點(diǎn)
if (strNum.indexOf(".") != -1) {
point = "元";
} else {
if (strNum.length() > 12) {
System.out.println("數(shù)字太大,不能完成轉(zhuǎn)換!");
return "";
}
point = "元整";
}
// 轉(zhuǎn)換結(jié)果
String result = getInteger(strNum) + point + getDecimal(strNum);
if (result.startsWith("元")) { // 判斷是字符串是否已"元"結(jié)尾
// 截取字符串
result = result.substring(1, result.length());
}
// 返回新的字符串
return result;
}
}
總結(jié)
以上是生活随笔為你收集整理的android 字体像素转换工具类_android工具类,转换大小写,保留小数点处理方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux 有名管道pipe,linux
- 下一篇: mysql数据表中取几列_MySQL实现