Very quick Java question

swatbat

[H]F Junkie
Joined
Apr 25, 2001
Messages
13,052
I'm trying to make a quick multiplication program and can not figure out how to set a constant varriable with a decimal in it. Can anyone help me with this.

Ie
float salestax

How would I define this as 0.055 so I can make it a %
I'm looking at like final salestax =

How do I set this up as a number. Do I need to put f after it and how do I enclose it so I can have the . in it?

Thanks
 
I don't know java but I do know a bit of C so..

couldn't you just say

float salestax = 0.055; ??

then you just multiply that by 100 then make a string variable concatenating "5.5" with a "%".
 
To use decimals you need to make it as a double. To make it a final just do public static final double salestax = 0.055; If you are wrapping primitive types as objects then you would use Double. I don't have much experience with floats so maybe I don't understand what you are trying to do. I would think you would want to do your calculations in primitive types but to set a float I think you would do public static final float salestax = 0.055f; which I think makes a double wrapped in a float object. Without the f on the end I think you will have a loss of precision because it will see the 0.055 as a double no the float you need it to be.
 
final double worked. Not sure why float didn't for me. I thought it allowed a decimal place.
 
Icemastr said:
...which I think makes a double wrapped in a float object.
Nope. float is a primitive data type. There is a Float wrapper class just like for any other primitive, but that's not what's going on here.

I don't have much experience with floats...
If you know all about doubles, then you know everything there is to know about floats. A float is a single-precision (32 bit) floating point number, a double is a double-precision (64 bit) floating point number. They're treated identically by Java, except for the casting rules.
 
Back
Top