Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8094177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:42:58+00:00 2026-06-05T20:42:58+00:00

I’m having an issue converting from Fahrenheit to Celsius in Java. I just started

  • 0

I’m having an issue converting from Fahrenheit to Celsius in Java. I just started learning Java, and am still working my way through books and tutorials.

For my first program, I’m trying to create a basic temperature calculator based on this tutorial.

I’m wanting to extend it’s functionality, and also include a Fahrenheit to Celsius conversion, as well as the base Celsius to Fahrenheit conversion.

So, I got the Celsius to Fahrenheit part working using the example code that was given, and now, using the same code, slightly modified after a lot of trial and error to get it to work, I’m trying to tackle the other half. I got it to work to some degree, but it’s not completely correct.

When I enter, for example, 89 degrees Fahrenheit, it returns 31 Celsius instead of the expected 32. I’m thinking this is an issue in the way the number is getting rounded, but I’m not sure how to go about fixing it. I’ve done some reading, and I think it might be able to be fixed by setting the RoundingMode to “HALF_UP”, but I’m not sure how to do it so that it affects only this conversion, and not the other one that works. This is what I have:

int tempCel = (int) ((int)((Double.parseDouble(tempTextFieldFC.getText())) 
         - 32) * .55555);
tempCelLabel.setText(tempCel + " ° Celsius"); 

Any help or suggestions that can be given will be greatly appreciated. Thanks for your time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T20:43:01+00:00Added an answer on June 5, 2026 at 8:43 pm

    Use Math.round() to perform rounding, instead of casting to int. Casting always rounds towards 0, as per JLS section 5.1.3:

    Otherwise, if the floating-point number is not an infinity, the floating-point value is rounded to an integer value V, rounding toward zero using IEEE 754 round-toward-zero mode (§4.2.3).

    Note that Math.round(double) is still declared to return a double, as it may be outside the range of int or long – but I would expect that not to be a problem in your case.

    I’d also advise you to split up your code into multiple statements instead:

    String fahrenheitText = tempTextFieldFC.getText();
    double fahrenheit = Double.parseDouble(fahrenheitText);
    double celsius = ((fahrenheit - 32) * 5) / 9;
    int celsiusRounded = (int) Math.round(celsius);
    tempCelLabel.setText(celsiusRounded + "° Celsius");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.