This question already has answers here:
Java: Why do you need to specify an 'f' in a float literal? [duplicate]
(3 answers)
Closed 6 years ago.
I am confused by the 'f'.
ObjectAnimator moveUp = ObjectAnimator.ofFloat(ivLogo, "translationY", 0f, -150f);
moveUp.setDuration(1000);
moveUp.setStartDelay(500);
moveUp.start();
What's the meaning of 'f'?And what's the meaning of form 'of' to '-150f'?
It's a floating point literal.
https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.2
0f means that 0 is float
The 'f' means that the -150 is a float type, not a value by itself
That f is needed because the parameter type the function expects is float. And that is the way of converting 0 (which is integer) to float.
See here
Also using d and L will give you double and long type respectively (just in case if you see them any other place.)
Related
This question already has answers here:
How do I print a double value without scientific notation using Java?
(18 answers)
Closed 11 months ago.
How can I show the full number instead of the scientific number when you perfonm a division this long.
9.34429093014885 / 10000000
I want to display:
0.000000034429093014885
But it keep returning:
9.34429093014885E-7
JAVA
Double value = ( price / exponent);
BigDecimal.valueOf(9.34429093014885 / 10000000).toPlainString()
Gives your expected result
This question already has answers here:
Dividing two integers in Java gives me 0 or 100?
(5 answers)
Closed 3 years ago.
I have something like this:
int mls = 400; // 400 milliliters
I'd like to show 0,4 for users (0,4... 0,8... 1,0 ... 1,2...)
I try to divide mls by 1000:
mls = 400 / 1000;
TextView todayValue = findViewById(R.id.todayValue);
todayValue.setText(String.valueOf(mls));
it gives me 0 as a result.
any ideas how to convert milliliters to liters showing to user something like 0,4 liters???
The idea is, you are first doing a / b, and because it's an integer operation, it'll round the result to 0. Doing a * 100 first should fix it.
From:Dividing two integers in Java gives me 0 or 100?
You should try it with another primitive type since int doesn't cover decimals.
Other options are, for instance, Double, Float, Long
So instead of declaring your variable as int mls = 400 you could do it as double mls = 400.
Another solution could be using a decimal at the operation, like 400.0 / 1000.0
This question already has answers here:
How to nicely format floating numbers to string without unnecessary decimal 0's
(29 answers)
Closed 9 years ago.
I've got a series of float numbers; I want to display them as they are;
but while I use String.getValueOf .0 will be added to my numbers
ie 10 is shows as 10.0
what should i do to prevent this mess ?
Try
if(stringVariable.endsWith(".0")) stringVariable = stringVariable.replace(".0" , "");
This way it will remove .0 only if the string ends with .0
Hope this helps.
You can change from float to int using Math.round()
using something like sprintf would do it
String.format("%.0f", value);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Round a double to 2 significant figures after decimal point
I know that there are plenty of examples on how to round this kind numbers.
But could someone show me how to round double, to get value that I can display as a
String and ALWAYS have 2 decimal places?
You can use String.format("%.2f", d), your double will be rounded automatically.
One easy way to do it:
Double d;
Int i;
D+=0.005;
i=d*100;
Double b = i/100;
String s = b.toString():
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why can't decimal numbers be represented exactly in binary?
i get very odd behaviour from simple float maths. eg.
//create a float with value of 1.0
float f = 1.0;
//take 0.1 from its value
f -=0.1;
for the first few times when i minus 0.1 it returns 0.9, 0.8, 0.7......
then for some reason it will return 0.699999999999, 0.59999999999 and so on.
to say this is unexpected is an understatement.
so to fix this i either need to know why it would do this
or a math function similar to Round(float) where it will round the number from 0.5999999 to 0.6.
thank you
edit,
ok sorry for asking lol
any fix available? like Round(float) kinda thing?
other edit:
for the next person to ask about this heres a fix
final DecimalFormat myFormat = new DecimalFormat("#.#");
myFormat.format(myFloatValue)
this will change myFloatValue 0.599999 into 0.6
A computer is a finite device, so it stores floating point numbers with a finite precision. And it stores them as binary floating point numbers -- that is relative to base 2 instead of base 10. A number with a finite representation as a decimal fraction doesn't necessarily have a finite representation as a binary number, so it must be rounded to be stored in a finite computer. In this example, 0.1 will be rounded to
0.1000000000000000055511151231257827021181583404541015625
when stored as a double precision floating point number, so you actually subtract a bit more than 0.1 in each step.
This is due to a fundamental limitation of the floating point representation. Certain numbers, such as 0.1, are not exactly representable using base-2 arithmetic with finite precision.