java 數字轉換字符串、保留小數位


  • 生成一個隨機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);
		    }
		}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM