英文:
How to convert a rational number to string with specific amount of decimals in Java?
问题
以下是翻译好的内容:
问题如下:在我们的Java RatNum类中,有一个int a(分子)和int b(分母),以及一些方法。对于这个项目,需要一个方法“toDotString”,这是我唯一没有解决的方法。该方法接受一个名为decimalCount的参数,然后返回一个向0舍入的值的字符串。因此,例如r=1/3,r.toDotString(3)应返回0.333。不允许使用BigDecimal、float、double等。
我已经成功解决了除以0.xxx开始的所有情况。
public String toDotString(int decimalCount){
int x = a/b;
String num=String.valueOf(x);
num= num + ".";
int rest=a-(x*b);
if(x!=0){
rest=(rest*(int)Math.pow(10,decimalCount))/b;
if(x<0){rest=rest*-1;}
}
String decimal=String.valueOf(rest);
if(rest==0){
for(int i=1; i<decimalCount; i++){
num=num + "0";
}
}
num= num + decimal;
return num;
}
这是适用于大于0.xxx的情况。
下面的代码片段是尝试修复它的内容。
if(x==0){
String denominator=String.valueOf(b);
boolean MultipleOfTen=true;
if(denominator.charAt(0)=='1'){
for(int i=1;i<denominator.length();i++){
if(denominator.charAt(i)!='0'){
MultipleOfTen=false;
}
}
}else{MultipleOfTen=false;}
if(MultipleOfTen){
for(int i=2; i<denominator.length(); i++){
num=num +"0";
}
}else if(a>(denominator.charAt(0)-'0')){
for(int i=2; i<denominator.length(); i++){
num=num +"0";
}
}else{
for(int i=1; i<denominator.length(); i++){
num=num +"0";
}
}
int k=decimalCount-(num.length()-2);
int dec=(int)Math.pow(10,(k+denominator.length()-1))/b;
String Decimal=String.valueOf(dec);
num=num+Decimal;
if(MultipleOfTen){num=num.substring(0,num.length()-1);}
if(a<0){num="-"+num;}
return num;
}
这是我尝试为x==0的情况做的,但它并不适用于所有经过测试的情况。我还感觉有一种更简单的方法可以完成所有这些,因此非常欢迎任何建议,谢谢!
英文:
The problem is as follows: In our RatNum class in Java there is an int a(nominator) and int b(denumerator) among some methods, for this project a method "toDotString" is required which is the only one I haven't managed solving. The method takes an argument decimalCount and then returns a string with the value rounded towards 0. So for example r=1/3, r.toDotString(3) should return 0.333. The use of BigDecimal, float, double, etc is not allowed.
I've managed to solve it for all cases except for the cases where it starts with 0 followed by decimal(0.xxx).
public String toDotString(int decimalCount){
int x = a/b;
String num=String.valueOf(x);
num= num + ".";
int rest=a-(x*b);
if(x!=0){
rest=(rest*(int)Math.pow(10,decimalCount))/b;
if(x<0){rest=rest*-1;}
}
String decimal=String.valueOf(rest);
if(rest==0){
for(int i=1; i<decimalCount; i++){
num=num + "0";
}
}
num= num + decimal;
return num;
}
This is what works for the cases above 0.xxx.
Next piece of code is what was tried to fix it.
if(x==0){
String denominator=String.valueOf(b);
boolean MultipleOfTen=true;
if(denominator.charAt(0)=='1'){
for(int i=1;i<denominator.length();i++){
if(denominator.charAt(i)!='0'){
MultipleOfTen=false;
}
}
}else{MultipleOfTen=false;}
if(MultipleOfTen){
for(int i=2; i<denominator.length(); i++){
num=num +"0";
}
}else if(a>(denominator.charAt(0)-'0')){
for(int i=2; i<denominator.length(); i++){
num=num +"0";
}
}else{
for(int i=1; i<denominator.length(); i++){
num=num +"0";
}
}
int k=decimalCount-(num.length()-2);
int dec=(int)Math.pow(10,(k+denominator.length()-1))/b;
String Decimal=String.valueOf(dec);
num=num+Decimal;
if(MultipleOfTen){num=num.substring(0,num.length()-1);}
if(a<0){num="-"+num;}
return num;
}
This is how I tried doing it for the x==0 case but it doesn't work for all cases tested. I also feel that there is a simpler way to do all of this so any recommendations are greatly appreciated, Thank you!
答案1
得分: 1
String.format(Locale.ENGLISH, "%.4f", 1. / 3); // 0.3333
英文:
String.format(Locale.ENGLISH, "%.4f", 1. / 3); // 0.3333
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论