Java求助Decimal format cannot be resolved to a type

标签 java decimalformat

我花了一段时间试图弄清楚为什么它不会在我的 java 代码中使用十进制格式。我已经用其他整数声明了十进制格式,但仍然出现错误。这是我的代码:

public class Assign31
{
  public static void main(String [] args)
  {
    DecimalFormat speedPattern = new DecimalFormat( "00.00" );
    int gearRadius = 100;
    double pi = Math.PI;
    int cadence = 90;

     double gearspeed = gearRadius * pi;

     double speedmph = gearspeed % cadence;

     System.out.println("The speed of the cyclist on a bike with gear 100.0 and cadence 90 is " + 
speedmph + " mph.");
     System.out.println("The speed (rounded) of the cycleist on the bike with gear 100.0 and cadence 90 is " + 
speedPattern.format(speedmph));
  }
}

最佳答案

你需要导入它:

import java.text.DecimalFormat;

public class Assign31 {
    public static void main(String [] args) {
        DecimalFormat speedPattern = new DecimalFormat( "00.00" );
        // etc.

关于Java求助Decimal format cannot be resolved to a type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14719318/

相关文章:

java - 当小数为 0 时转换 DecimalFormat.parse() 的结果时出现 ClassCastException

java - 用户正在 JAVA 中使用 SMACK api 键入通知

java - 如何使用 CDI 从外部库注入(inject) bean?

java - mapstruct 如何将对象列表转换为接口(interface)列表?

java - 启动通过 Apache Cayenne ORM 返回数据的存储过程不起作用

java - Android Studio 中的 appLocale 设置

java - 在android Java中格式化 float

Java DecimalFormat 在格式化 double 时失去精度

c# - Java DecimalFormat 的 c# 等价物是什么?

java - Java 中的 ceil 或小数点后四舍五入