3
所以,我正在制作一个程序,其中我有用户插入分子和分母,程序将伪分数转换为小数。它工作正常,只是一件事。一个,如果我输入一个重复小数的分数(例如1/3,0.3333333 ...),我想要么它说0.33重复,要么是无理数,它会围绕它,让我们说7个数字,并且然后停下来,然后有“......非理性”。我怎么能这样做?代码如下。在Java中舍入小数
package Conversions;
import java.util.*;
public class FractionToDecimal {
public static void main (String[] args) {
Scanner sc = new Scanner (System.in);
System.out.println("Enter Numerator: ");
int numerator = sc.nextInt();
System.out.println("Enter Denominator: ");
int denominator = sc.nextInt();
if (denominator == 0) {
System.out.println("Can't divide by zero");
}
else {
double fraction = (double)numerator/denominator;
System.out.println(fraction);
}
}
}
@Reimeus编辑。谢谢。 –
因此不适合让别人为您编写代码。你需要做自己的作业。继续工作,当你有更具体的问题时,随时提问。 –
http://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java的副本 –