-
生成一個隨機100內小數,轉換為保留兩位小數的字符串,不考慮四舍五入的問題。
-
參考答案:
public class Test5 {
public static void main(String[] args) {
double random = Math.random()*100;
System.out.println("隨機數為:");
System.out.println(random);
String str = random+"";
int index = str.indexOf(".");
//System.out.println(index);
String substring = str.substring(0, index + 3);
System.out.println("轉換為:");
System.out.println(substring);
}
}