Android - Round to 2 decimal places [duplicate] - android

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

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

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(".","");

displaying float in android without adding trailing zero [duplicate]

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

How to print a double with two decimals in Android? [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 6 years ago.
Maybe this is a silly question, but I cannot guess how to solve it if it's not creating a method. Maybe there's a "natural way" to do it, like in C for example. Here's the problem:
I have a var:
double a;
And I want to show it only with 2 or 3 decimals. When I try to show it:
Text.setText("Value of a: " + String.valueOf(a));
It gives something like:
Value of a: 5.234966145
And i would want just
Value of a: 5.23
Without changing the real value of a so it shows the approximate number but works with the real number.
yourTextView.setText(String.format("Value of a: %.2f", a));
For Displaying digit upto two decimal places there are two possibilities -
1) Firstly, you only want to display decimal digits if it's there.
For example - i) 12.10 to be displayed as 12.1, ii) 12.00 to be displayed as 12. Then use-
DecimalFormat formater = new DecimalFormat("#.##");
2) Secondly, you want to display decimal digits irrespective of decimal present For example -i) 12.10 to be displayed as 12.10. ii) 12 to be displayed as 12.00.Then use-
DecimalFormat formater = new DecimalFormat("0.00");
You can use a DecimalFormat, or String.format("%.2f", a);
Before you use DecimalFormat you need to use the following import or your code will not work:
import java.text.DecimalFormat;
The code for formatting is:
DecimalFormat precision = new DecimalFormat("0.00");
// dblVariable is a number variable and not a String in this case
txtTextField.setText(precision.format(dblVariable));
textView2.setText(String.format("%.2f", result));
and
DecimalFormat form = new DecimalFormat("0.00");
textView2.setText(form.format(result) );
...cause "NumberFormatException" error in locale for Europe because it sets result as comma instead of point decimal - error occurs when textView is added to number in editText.
Both solutions are working excellent in locale US and UK.

android strange float maths [duplicate]

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.

Categories

Resources