There is no specific type suitable, for this case some can be used.
In fact, if you’re going to create types to handle these temperatures and you’re going to use a sophisticated infrastructure to represent them, the internal type that holds the numbers doesn’t matter much. It could use some strategies and depends on what you expect to accomplish. It may even be that a simple float
solve easily.
If you’re going to use a generic type to represent you can even use a int
, but probably the easiest is to use a float
which already has the decimal part. With a int
would have to always make a division to take the decimal part, do accounts to suit the scale you are using, have to take various care and should not be worth it for basic use and for more sophisticated use I think the previous solution is better.
Nothing prevents you from using a double
, but I find this kind of thing exaggerated. The float
saves memory and has more than enough precision for something simple like this. Can not say that is wrong, but would not be the first choice.
The difference between them is just the same precision, which obviously makes the most accurate (8 bytes versus 4). Do not confuse with accuracy.
If you had the statement to use something that takes up less space, you might even think about other things, even more if you have exactitude requirements. It seems to me that none of this is required, then float
is better in real use, but it can be argued that the double
no harm but to occupy memory for no reason, which changes little in most cases.
In this case I doubt that the BigDecimal
is necessary, but only you can answer if it must have accuracy beyond the precision that the float
already gives. See more on What is the correct way to use the float, double and decimal types?.
you can find a great explanation here.
– Gabriel Gonçalves
thank you Gabriel!
– Carol M
I think it’s worth asking the teacher why float is "wrong" in this case. At first, temperatures do not require such great precision that a float cannot withstand. Unless there is some other detail in the exercise that "forces" you to use double (or makes it the best choice). But it looks like you’re using the Wrappers (Double and Float, uppercase), it may have to do with the answer. If you want [Edit] the question putting more details, such as the code snippet for example, who knows we can give more details on this specific case
– hkotsubo
And after asking the teacher, comment here what she said, which is for us to see if it is true. Not doubting her, of course, but just to be on the "safe" side. I have seen a lot of teachers (from renowned college, including) who clearly only read theory, but never had to do anything "real", by the type of class given.
– Bacco
@hkotsubo I think you misunderstood because "the teacher when correcting the exercise in class ended up using the
Float
! "– ramaral
@Yeah, what a distraction for me. Even so, any of the types would serve and it remains strange that one of them is "wrong" and needs to be "corrected". Unless there is some detail in the exercise that "forces" to use float.
– hkotsubo
@hkotsubo Nothing obliges, however there is justification to choose
float
as explained in the reply of Manieiro and Victor.– ramaral