• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Fahrenheit to Celcius

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

How can i show it as -17.77 instead of -17.77777777777778C?


Fahrenheit Celsius
=========================
0F = -17.77777777777778C




 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use DecimalFormat with an appropriate pattern.
Recommended reading http://docs.oracle.com/javase/tutorial/i18n/format/decimalFormat.html
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maneesh Godbole wrote:Use DecimalFormat with an appropriate pattern.


Or possibly String.format() or System.out.printf() (which uses the same type of format string).
They're a bit "geekier", but if you've used C or C++ before, they may be familiar.

@Serkan: And just FYI, there's another formula for temperature conversion that isn't as well known, but I find much simpler to remember:
  C = (F + 40) * (5.0 / 9.0) - 40

Why? Because the only thing that changes if you want to convert the other way is the factor, viz:
  F = (C + 40) * (9.0 / 5.0) - 40
whereas with the '± 32' approach you also have to remember whether to add or subtract and when.

HIH

Winston
 
Marshal
Posts: 76822
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The descriptions of format Strings in the Java™ Tutorials (2nd Java™ Tutorials link) may be easier to understand than the links Winston gave you. I would agree with Winston: use the % tags.

You cannot get 0°F to display as −17.77°C because it isn't. It is −17.78°C.
 
Campbell Ritchie
Marshal
Posts: 76822
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A few minutes ago, I wrote:. . . You cannot get 0°F to display as −17.77°C because it isn't. It is −17.78°C.

Actually you can, but only by using an unconventional rounding mode.
 
Campbell Ritchie
Marshal
Posts: 76822
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
dhrubo bhattacharjee, please read what it says on the contents page for this forum:-

We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.

It does not help the posters to learn if you simply post an answer like that. One can learn best by working out one's own answer.

Please don't be annoyed with me, but I have pulled rank and deleted your post.
 
Greenhorn
Posts: 27
Eclipse IDE Oracle Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:dhrubo bhattacharjee, please read what it says on the contents page for this forum:-

We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.

It does not help the posters to learn if you simply post an answer like that. One can learn best by working out one's own answer.

Please don't be annoyed with me, but I have pulled rank and deleted your post.




I am sorry, I will keep this in mind while replying to the posts in the future.

Thanks
 
Campbell Ritchie
Marshal
Posts: 76822
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apology accepted
 
Serkan Kilic
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all. I guess i figured out how to do it.Looks like it works.

 
Bartender
Posts: 2908
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Serkan Kilic wrote:Thank you all. I guess i figured out how to do it.Looks like it works...


You might need a few better variable names instead of C, F, etc... Read the java conventions here : http://www.oracle.com/technetwork/java/codeconventions-135099.html
Basically, don't use caps for naming them and give them a meaningful name. Next, method name should be an action / verb. So I suggest "convertFarenhietToCelsius" instead of "celsius". Your class name should be meaningful, don't use abbreviations such as "Temp"
 
salvin francis
Bartender
Posts: 2908
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's my suggestion for your program:

 
Campbell Ritchie
Marshal
Posts: 76822
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:. . .

Float? Never use floats unless some other API forces you to. Use doubles. And wouldn't that method be static?
 
Campbell Ritchie
Marshal
Posts: 76822
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Serkan Kilic wrote:// ...

Yes, that will print -17.78 for 0°F. But only use \n if somebody has told you they require the LF character. In printf, always use %n instead.
 
salvin francis
Bartender
Posts: 2908
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Float? Never use floats unless some other API forces you to. Use doubles. And wouldn't that method be static?


Lets say I would agree over double... but why static ?
 
Campbell Ritchie
Marshal
Posts: 76822
366
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Becaue that method is a function. It takes all its requisite information from the parameters and passes all information passed on via its returned value. That means that in the most dubious classification of methods known to modern science, your method is a 1368. It requires no information from anything in the object and records no information into anything in the object (i.e. fields). It can therefore be static.
 
salvin francis
Bartender
Posts: 2908
150
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Becaue that method is a function...


Interesting.. Yup you are right.
I think the best way to describe such method would be ... Stateless
 
Saloon Keeper
Posts: 14490
325
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The correct term is "pure function".
 
Saloon Keeper
Posts: 5157
207
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:You might need a few better variable names instead of C, F, etc... Read the java conventions here
(...)


C and F are very well known symbols for Celcius and Fahrenheit, so I find
OP's methods very clear. Java's naming conventions are not strict rules.
Do you really think that 'fahrenheitValue' makes the code easier to read?
 
salvin francis
Bartender
Posts: 2908
150
Google Web Toolkit Eclipse IDE Java
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:Do you really think that 'fahrenheitValue' makes the code easier to read?


Yes I do , that's why I used that in my program.

This is a quote from java code conventions :

Variable names should be short yet meaningful. The choice of a variable name should be mnemonic- that is, designed to indicate to the casual observer the intent of its use. One-character variable names should be avoided except for temporary "throwaway" variables. Common names for temporary variables are i, j, k, m, and n for integers; c, d, and e for characters.


Conventions exist so that another programmer reading your code understands it. Most of the time the code you write is maintained by someone else. Lets not make his life miserable

Another quote from the same site:

Naming conventions make programs more understandable by making them easier to read. They can also give information about the function of the identifier-for example, whether it's a constant, package, or class-which can be helpful in understanding the code.

 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:C and F are very well known symbols for Celcius and Fahrenheit, so I find OP's methods very clear...


While I'm normally a stickler for following the guidelines, I'm going to side with Piet here....But only just.

F and C are used often enough (albeit incorrectly - the SI symbol 'F' is for farads, and 'C' for coulombs) to avoid any ambiguity in context and, while single-letter names are generally frowned on, let alone using caps, here I would say that they're as "descriptive" as you can make them.

I add a corollary of my own to the conventions:
While trying to make names as descriptive as possible, they should also be as short as possible. So
  • In the case of two equally descriptive names, choose the shorter one.
  • Avoid unnecessary components.

  • @salvin: And on that basis I'd say that 'fahrenheitValue' is overkill. You know from the type, and the way it's used, that the variable is a value, so why not just call it 'fahrenheit'?

    If you'd done that, I wouldn't have written this post, because then I think it comes down to individual taste.

    My 2¢.

    Winston
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    salvin francis wrote:Another quote from the same site:

    ...They can also give information about the function of the identifier-for example, whether it's a constant, package, or class-which can be helpful in understanding the code.


    And my take on that: They can, but they shouldn't...at least not in Java.

    Things like Hungarian Notation were fine in the days of monolithic code, or in languages that support indistinct types, but Java doesn't. It is strongly typed, and if you follow the conventions, your reader should have enough information to follow what names refer to.
    And for stuff like distinguishing between classes and interfaces (if you really feel it's needed), you have Javadoc.

    You won't be getting any 'IList' interfaces from this chap.

    Winston
     
    Campbell Ritchie
    Marshal
    Posts: 76822
    366
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Joel Spolsky has his own opinion about Hungarian notation(=HN). I think it is here. He says there are two kings of HN, one good the other bad.
     
    salvin francis
    Bartender
    Posts: 2908
    150
    Google Web Toolkit Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Winston Gutkowski wrote:While I'm normally a stickler for following the guidelines, I'm going to side with Piet here....But only just.


    :'(

    Winston Gutkowski wrote:
    @salvin: And on that basis I'd say that 'fahrenheitValue' is overkill. You know from the type, and the way it's used, that the variable is a value, so why not just call it 'fahrenheit'?


    fahrenheit is okay no issues there (even a self made shorter acronym like fahrenht seems good as long as someone reading it understands). 'F' I feel is bad (My opinion). I would definitely frown on it if I saw it in some code.

    lets agree to disagree

     
    Campbell Ritchie
    Marshal
    Posts: 76822
    366
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    salvin francis wrote:. . . a self made shorter acronym like fahrenht . . .

    That is not an acronym (pedant!) and looks to me very prone to misspellings, so I would take a disliking to that variable name.
     
    salvin francis
    Bartender
    Posts: 2908
    150
    Google Web Toolkit Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:That is not an acronym ....


    Mr. pedant, do you like C and F ?
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    salvin francis wrote:Mr. pedant, do you like C and F ?


    I do (as you already know), and let me tell you why. The vast majority of Serkan's program is dealing with a formula, and those names are 'algbraic'.

    To me:practically documents itself. And for that, I'm willing to break a convention or two.

    Winston
     
    Marshal
    Posts: 8493
    610
    Mac OS X VI Editor BSD Linux
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Winston Gutkowski wrote:practically documents itself. And for that, I'm willing to break a convention or two.

    Partially agree.

    Just being nitpicky - method convertToCelsius name does not reveal its full intention, and nothing related with Fahrenheit is searchable in the code.
    On the other hand, fahrenheitValue might overkill a bit, but fahrenheit just good in my opinion OR if to go with C and F, method name would require improve its name to convertFahrenheitToCelsius.
     
    Piet Souris
    Saloon Keeper
    Posts: 5157
    207
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Indeed. (edit: indeed to Winstons reply)

    The reason for starting this discussion was Winstons now famous formula:

    C + 40 = 5/9 (F + 40)

    and I bet that everyone recognizes this at first glance.

    If that's true for formulas on paper, then why not for code as well?

    In financial maths the letters i, 'are' (spellingchecker!) and d are widely
    used symbols in formula's. I do not hesitate to code these formula's
    with these letters as well. Firstly, the code then resembles the specifications,
    secondly, a programmer getting confused by this is probably not the right man
    to maintain the code.
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Liutauras Vilda wrote:...if to go with C and F, method name would require improve its name to convertFahrenheitToCelsius...


    Or indeed just fahrenheitToCelsius(); but yup, I'll agree with you there.

    Winston
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    And just to poke the fire: has anyone else found the fact that 'for' is a keyword as frustrating as hell?

    I've written innumerable classes where I would have loved to call its factory method for(...), eg:
      ArrayView<String> view = ArrayView.for("Hello", "World");
    but of course you can't; so I usually fall back on 'of(...)' instead.

    But I do remember one case where "for" seemed so appropriate (and "of" not) that I actually called the method 'For()' - with copious docs for course.

    Winston
     
    What's a year in metric? Do you know this metric stuff tiny ad?
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic