啊我眼殘弄錯了,下面不是java
的寫法,是javascript
的……
思路上應該是類似的,你可以參考看看
可以當字串用正規表達式玩,雖然感覺有點繞,當練習XD
// 原本的字串
let originString = '1234567899';
/**
* 去掉後三碼:
* $1 = (\d*): 任意數量的數字0-9
* $2 = (\d{2}): 兩個數字0-9
* $3 = (\d{3}): 三個數字0-9
*/
let newString = originString.replace(/(\d*)(\d{2})(\d{3})/, "$1.$2");
// 加 ,
let result = parseFloat(newString).toLocaleString();
console.log(result);
public class HelloWorld{
public static void main(String []args){
String sA = "1234567899";
String sB = "12345678999";
String sTA = String.format("%1$,.2f", Double.parseDouble(sA.substring(0, sA.length() - 3)) / 100);
String sTB = String.format("%1$,.2f", Double.parseDouble(sB.substring(0, sB.length() - 3)) / 100);
System.out.println(sTA);
System.out.println(sTB);
}
}