获取方法的答案到小数点后两位 [英] Getting answer of method to two decimal places

查看:70
本文介绍了获取方法的答案到小数点后两位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用下面的代码来获取我在两个分解位置创建的方法的答案.但是,当我这样做并顺从时,我得到一个错误,提示期望标识符. 2错误出现,一个指向2,另一个指向之前.我有什么问题?

I am using the code below to get the answer of a method i created into two decomal places. But when I do this and complile i get an error saying identifier expected. 2 error come up one pointing at the 2 and the other just before. what is my problem?

  import java.text.NumberFormat;

  NumberFormat nf = NumberFormat.getNumberInstance();
 nf.setMaximumFractionDigits(2);

推荐答案

您所显示的内容是正确的,假设所有行都不在一起(import语句必须在任何类的外部).因此,例如,这是有效的:

What you've shown is correct, assuming that the lines aren't all together (import statements must be outside of any class). So for instance, this is valid:

import java.text.NumberFormat;

class MyClass {

    void someMethod() {
        NumberFormat nf = NumberFormat.getNumberInstance();
        nf.setMaximumFractionDigits(2);
        // ...
    }
}

...但是问题中显示的那几行不是.

...but those lines together as shown in your question are not.

如果不是这样,您就说错误似乎集中在2上.有时,当我们在SO上看到类似这样的问题时,是因为某些零宽度或类似空格的特殊字符意外地出现在源代码中.因此,如果您删除该行并重新输入,则可能会删除令人反感的字符. (实际上令人惊讶的是它出现的频率.)

If that's not it, you said that the error seems to focus on the 2. Sometimes when we see questions like this here on SO, it's because some zero-width or space-like special character has accidentally ended up in the source. So it may be that if you delete that line and retype it, you'll wipe out the offending character. (It's actually surprising how often that comes up.)

这篇关于获取方法的答案到小数点后两位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆