Sorry for the title name but this is probably a funny question. i need to have this like it is. I have an array of floats in the format [10.0, 20.0, 30.0, 40.0, 50.0]
i need the array to be represented like [10.0f, 20.0f, 30.0f, 40.0f, 50.0f]
. I have tried using array List to convert and add the notation and convert back.. but no matter what i try, i still get a float array that looks like a double. Any ideas Please?
even tried something like this:
ArrayList<String> valuelist = new ArrayList<String>();
for(int z = 0; z < mValues.length; z++){
valuelist.add(Float.toString(mValues[z]) + "f");
} //its fine here but later changes
Object[] objarray2 = valuelist.toArray();
String[] stringfloat = Arrays.copyOf(objarray2, objarray2.length, String[].class);
float[] myfloat = new float[stringfloat.length];
for(int j =0; j< myfloat.length; j++){
//myfloat[j] = Float.parseFloat(stringfloat[j]);
myfloat[j] = Float.valueOf(stringfloat[j]);
}
f
is used to tell the compiler that treat this as float number. If you want your array in this format use an array of string and append f
in each element
As per JLS :
A floating-point literal is of type float if it is suffixed with an ASCII letter
F
orf
; otherwise its type is double and it can optionally be suffixed with an ASCII letterD
ord
(§4.2.3).
Keep the original array where members are float and just create a "String myToString()" method to print it like you want, with the +"f". I mean, create a personalized method to print the array like you want it to appear.
I assume you just want the print out of the array of float to "appear" with a final f. The representation in the machine of the number will not change with an f at the end or not. The final f in the string is useful when parsing a number, so you the parser knows which kind of number it is, if you already do not tell it.
You have:
myfloat[j] = Float.valueOf(stringfloat[j]);
Here's Float.valueOf(String)
:
public static Float valueOf(String s) throws NumberFormatException
{
return new Float(FloatingDecimal.readJavaFormatString(s).floatValue())
}
As you can see, it returns a Float
, which is created through Float(float)
constructor, which is simply:
public Float(double value)
{
this.value = (float) value;
}
The readJavaFormatString(String)
converts the String
to a double
, and .floatValue()
converts the double
, wrapped in a FloatingDecimal
to a float
, which is passed to the above constructor. So, im short, you do have a float
.
NB:
float num1 = XYZ;
float num2 = XYZf;
Despite the different notation, num1
and num2
compile to the same bytecode (where XYZ
are some numbers, say 123); however, you cannot make an assignment of a number ending in an f
to say an int
. Additionally, if you try to put an int
into a float, you do not need the f
at the end, but if your number has any numbers to the right of the radix, you need to append the f
so that it will compile with a possible loss of precision
error.
Also, you really shouldn't be using float
unless there's a good reason to do so, especially if you're planning to use ==
for comparison; instead, use double
. float
and double
are essentially the same thing, except that double
is 64-bits, whereas a float
is 32-bits, so a double
is more precise.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.