如何在Java中将有理数转换为指定小数位数的字符串?

huangapple go评论61阅读模式
英文:

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 + &quot;.&quot;;
int rest=a-(x*b);
if(x!=0){
rest=(rest*(int)Math.pow(10,decimalCount))/b;
if(x&lt;0){rest=rest*-1;}
}
String decimal=String.valueOf(rest);
if(rest==0){
for(int i=1; i&lt;decimalCount; i++){
num=num + &quot;0&quot;;
}
}
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)==&#39;1&#39;){
for(int i=1;i&lt;denominator.length();i++){
if(denominator.charAt(i)!=&#39;0&#39;){
MultipleOfTen=false;
}
}
}else{MultipleOfTen=false;}
if(MultipleOfTen){
for(int i=2; i&lt;denominator.length(); i++){
num=num +&quot;0&quot;;
}
}else if(a&gt;(denominator.charAt(0)-&#39;0&#39;)){
for(int i=2; i&lt;denominator.length(); i++){
num=num +&quot;0&quot;;
}
}else{
for(int i=1; i&lt;denominator.length(); i++){
num=num +&quot;0&quot;;
} 
}
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&lt;0){num=&quot;-&quot;+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, &quot;%.4f&quot;, 1. / 3);    // 0.3333

huangapple
  • 本文由 发表于 2020年9月21日 23:06:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63995041.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定