You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java华氏转摄氏循环程序问题:如何输出带小数的摄氏温度?

Fixing Decimal Place Display for Celsius Temperature in Your Java Program

Hey there! Let's figure out why your Celsius temperature outputs aren't showing decimal places and fix it up quickly.

The Root Cause

The most common reason this happens is integer division in Java. If you're using int (integer) variables for your calculations, Java will automatically discard any fractional part of the result. For example, if you have code like this:

int fahrenheit = 77;
int celsius = (fahrenheit - 32) * 5 / 9; // Result is 25 instead of 25.0

The integer operation strips away all decimal values, leaving you with whole numbers only.

Solutions to Show Decimal Places

Here are three straightforward ways to fix this:

  1. Use Floating-Point Variables for Calculations
    Switch your Celsius variable to a double (or float) and use floating-point literals (like 5.0 instead of 5) in your formula to trigger floating-point division:

    double celsius = (fahrenheit - 32) * 5.0 / 9.0;
    

    This ensures the calculation keeps fractional values instead of truncating them.

  2. Add Explicit Type Casting
    If you need to keep some variables as integers, cast one part of the calculation to a double to force floating-point math:

    int fahrenheit = 77;
    double celsius = (double)(fahrenheit - 32) * 5 / 9; // Casts the difference to double first
    

    The cast converts the integer result of fahrenheit - 32 to a double, so the rest of the division will preserve decimals.

  3. Format Output to Specify Decimal Places
    Even if you have a floating-point result, Java might not always show trailing decimals by default. Use System.out.printf() (or String.format()) to define exactly how many decimal places to display:

    // Example: Show 2 decimal places
    System.out.printf("Fahrenheit: %d → Celsius: %.2f%n", fahrenheit, celsius);
    

    The %.2f format specifier tells Java to format the floating-point number with 2 decimal places. You can change 2 to any number (like %.1f for one decimal, %.3f for three) based on your needs.

Full Example Code

Here's a complete working version that combines these fixes:

public class TemperatureConverter {
    public static void main(String[] args) {
        // Loop through a range of Fahrenheit temperatures
        for (int fahrenheit = 0; fahrenheit <= 100; fahrenheit += 10) {
            // Calculate Celsius with floating-point math
            double celsius = (fahrenheit - 32) * 5.0 / 9.0;
            // Format output to show 2 decimal places
            System.out.printf("Fahrenheit: %3d → Celsius: %.2f%n", fahrenheit, celsius);
        }
    }
}

Expected Output

Running this code will give you output like:

Fahrenheit: 0 → Celsius: -17.78
Fahrenheit: 10 → Celsius: -12.22
Fahrenheit: 20 → Celsius: -6.67
Fahrenheit: 30 → Celsius: -1.11
Fahrenheit: 40 → Celsius: 4.44
Fahrenheit: 50 → Celsius: 10.00
Fahrenheit: 60 → Celsius: 15.56
Fahrenheit: 70 → Celsius: 21.11
Fahrenheit: 80 → Celsius: 26.67
Fahrenheit: 90 → Celsius: 32.22
Fahrenheit: 100 → Celsius: 37.78

内容的提问来源于stack exchange,提问作者Marc

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:03:02