练习BigDecimel and Regular Expression
程序员文章站
2022-04-05 10:29:12
...
程序需求:http://joezxy.iteye.com/blog/366794
import java.math.BigDecimal;
public class Algorithm {
public static void main(String[] args) {
powerWrapper(95.123,12);
powerWrapper(0.4321,20);
powerWrapper(5.1234,15);
powerWrapper(6.7592,9);
powerWrapper(98.999,10);
powerWrapper(1.0100,12);
}
public static void powerWrapper(double x, int y){
String result = power(x,y);
System.out.println(format(result));
}
public static String power(double x, int y){
BigDecimal b1 = new BigDecimal(String.valueOf(x));
return b1.pow(y).toPlainString();
}
public static String format(Object x){
String regex1 = "^0\\..*$";
String regex2 = "^.*\\.0*$";
String regex3 = "^0\\.0*$";
String result = String.valueOf(x);
if(result.matches(regex3)){
result = "0";
}
else if(result.matches(regex1)){
result = result.replace("0.", ".");
}
else if(result.matches(regex2)){
int index = result.indexOf('.');
result = result.substring(0, index);
}
return result;
}
}
上一篇: P50系列尝鲜!一文了解华为新版鸿蒙OS:相机解锁更多玩法
下一篇: 生成随机日期
推荐阅读
-
C# regular expression to validate email
-
Python使用正则表达式(Regular Expression)超详细
-
浅谈正则表达式(Regular Expression)
-
C# regular expression to validate email
-
python re正则表达式模块(Regular Expression)
-
共享日常收集JS正则表达式(JavaScript regular expression)
-
正则表达式Regular Expression (RegExp)详解
-
正则表达式regular expression详述(一)
-
正则表达式regular expression详述(二)
-
正则表达式基础教程 regular expression第1/2页