This question already has answers here:
Java: double: how to ALWAYS show two decimal digits
                                
                                    (6个答案)
                                
                        
                                5年前关闭。
            
                    
int ptstotal, ptsearned, ptssofar;
ptstotal= 1500;
ptsearned= 750;
ptssofar= 950;

System.out.println("The current percentage is "+(int)Math.round(ptsearned*1)/(double)(ptssofar)*100+"%.");

System.out.println("The current percentage is "+Math.round(ptsearned*1)/(double)ptssofar*100+"%.");


输出是一个长十进制78.96736805263%,只需要78.97%就需要一些帮助

最佳答案

尝试改用printf

double value = (int)Math.round(ptsearned*1)/(double)(ptssofar)*100;
System.out.printf("The current percentage is %.2f %",value);

10-06 14:32