displaying float in android without adding trailing zero [duplicate] - android

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);

Related

How to display number with all decimal (not Science notation) android? [duplicate]

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

Android Animation 'f' meaning [duplicate]

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.)

Remove decimal point and numbers from string in Android [duplicate]

This question already has answers here:
How to remove the " .0" in a whole number when using double in java? [duplicate]
(3 answers)
Closed 4 years ago.
So, I have two numbers as strings - for example, 123.00 and 123.50. How do I remove the decimal point and all that follows. I had a method that worked for the 123.00, but would not work correctly on the 123.50.
Here is what I have so far:
String balance = getString("balance");
BigDecimal number = new BigDecimal(balance);
String formattedBalance = number.stripTrailingZeros().toPlainString();
int roundedBalance = Integer.parseInt(formattedBalance);
int roundedBalance = Integer.parseInt(getString("balance").split(".")[0]);
Do you want to remove the following points or numbers?
If only the points why not:
balance = balance.replace(".","");

What does f means in animation [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does suffix ‘f’ mean in Android programming?
Excuse me if it was a stupid question, but I always see people writing something like 350f and 15f when initializing animations (passing as paramter) . What does f mean?
Thank you
I believe it's the format of the argument, FLOAT in this case.

Android - Round to 2 decimal places [duplicate]

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():

Categories

Resources