java - 四舍五入java的调用方法

标签 java methods casting formatting

好的,所以我不明白为什么它说该方法未在本地使用...。私有(private) String formatNumber() 方法是这样说的。

基本上我需要做的是有一个返回周长的方法 - 另一种将数字四舍五入到小数点后两位并返回字符串的方法 - 以及另一种返回圆周率格式化版本的方法...

不难看出我正在尝试做什么,但它给了我上述错误,我无法弄清楚。

//figures out circumference
public  double getCircumference(){

    circumference = 2 * Math.PI * radius;

    return circumference;


}
    //takes string and turns back into a double
public double getFormattedCircumference(){

    double x = Double.parseDouble(format);
    return x;


}
//this method is giving the error of not being used locally...
    //method takes double and turns to string so that it can be formatted and it
      has to be a string
private String formatNumber(double x){

    x = circumference;
    NumberFormat number = NumberFormat.getNumberInstance();
    number.setMaximumFractionDigits(2);
    String format = number.format(x);
    return format;
}

最佳答案

您已经声明了私有(private)方法,但您没有在当前代码中的任何地方使用它,因此编译器会警告您这一点(检查您的程序以查看您是否在任何地方调用此方法)。

顺便说一句,您看到的是警告,而不是错误。您的代码应该仍然可以编译,并且程序仍将运行(如果不存在错误)。


编辑 1
该方法有一个严重的问题,可能不止一个,因为它接受一个双参数然后立即丢弃它。为什么?如果您想格式化作为参数传入的数字,那么您不想丢弃该参数。另外,是否要将此方法设为 public 以便该类之外的对象可以调用它?另外,该方法是有状态的还是无状态的?它会使用类的字段,还是只会格式化传递给它的数字。如果是后者,那么它应该是一个static 方法。

关于java - 四舍五入java的调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12964483/

相关文章:

java - RxJava : how to handle combineLatest() when one of the streams emits nothing

java - 使用 Cron Job 将 .war 中的文件替换为另一个文件

python - 如何在类初始化期间添加在字符串中编码的动态方法?

c - 将一般深度的嵌套指针传递给 C 中的函数

java - 如何持久化哈希表

使用 Jsoup 需要登录的 Java 抓取网站

c++ - static_cast<T>(...) 是编译时还是运行时?

Java - Enumerable.Cast() 像 C#?

python - 方法列表

C++ map<char, static method pointer>?