Remove tailing ".0" from prices - java

How I can remove ".0" from my Price number from my android studio app?
MyApp save orders in database, then user can place order, after that server app get the orders placed.
I'm also using Retrofit2.
I have double prices 2000.0.
I want them without tailing 0, just 2000.
There are other sections with this problem, which I guess some guide about how to fix this part can help me to fix other parts.

There are 2 ways you can solve this.
(Adding to Andreas comment)
Either use int instead of double altogether.
Or you can always cast your double value in int.
double x = 1000.324;
System.out.println("double x = "+x);
System.out.println("int x = "+(int) x);
prints:
double x = 1000.324
int x = 1000

You could use :
new DecimalFormat("#").format(2000.0);
The result is "2000" without decimals.

There are a few things you can do do remove the numbers after the decimal and it depends on what you are trying to achieve.
You can cast them to an Int or use new DecimalFormat("#").format(value) which will remove anything after the .
You can use the Math.rint() or Math.round functions this will round to the closest integer 3.99 will become 4

Simply typecast your double value into int
double percentageValue=200.0;
System.out.println("remove value of after decimal point=="+(int)percentageValue);
and set value in android just like this:-
txt_product_price.setText(String.valueOf((int)percentageValue));

simply convert the Double value to Integer
Double x = 2000.0;
Integer y = (int) x;
Log.e("Integer format",String.valueOf(y));
and the Log is 2000

Related

How to round double to integer (without show auditor and zero number)?

How I can round the numbers of double variable when are was equal your integer values?
Indeed I want to change numbers example 4.0 or 8.0 to 4 or 8 without show the auditor and zero number. How to do it?
Just cast it:
double doubleNum = 4.0;
int num = (int) doubleNum; // 4
Suppose you have a variable bla, when you want to use bla just do (int)bla

In Java how does one convert from declared int value then print out decimal approximations

This is to say that the int are put into a System.out.println(); method that divides them up. But the value is a whole number, how does one take what's outputted as a whole number and get a decimal value from the declare int values?
Thank you
Well there's a couple ways, but you just cast it:
int myInt = 42;
System.out.println((double) myInt);
//Formatter
System.out.printf("%.2f\n", (double) myInt); //control output, e.g. 2 decimal places
I believe the problem is that you have two integer values and you want to merge them to become one number, with one of the 2 integers in the decimal part?
Well, if that is your question, then you can solve it by a simple while loop.
double wholePart = (double) userInput;
double decimalPart = (double) userInput;
while(Math.abs(decimalPart)>=1)
decimalPart /= 10;
System.out.print(Math.sign(decimalPart * wholePart) (Math.abs(wholePart) + Math.abs(decimalPart)));
Edit2: Using absolute values

How to get java to produce decimal points while dividing

I'm making a basic calculator where you can plus, times, divide and minus as i was experimenting to see if it worked i noticed that instead of 5 divided by being equal to 1.25 it only displayed 1.
Here's the code i use to handle the math problems:
if (box.getSelectedItem().equals(divide)){
JOptionPane.showMessageDialog(null, Integer.parseInt(first.getText()) / Integer.parseInt(second.getText()), "Answer", -1);
main(args);
}
Is there code that displays the decimal points as well?
Since you are using Integer,it is happening.
Use Double to preserve decimals.
In your case,use
Double.parseDouble(first.getText()) / Double.parseDouble(second.getText())
Integer division will give you Integer. Try using Double or BigDecimal data type.
You need to do the casting
(double)parseInt(first.getText()) / (double)parseInt(second.getText())
Int/Int will give you an Integer. So you need to cast it to Double to get the result in decimal.
EDIT:
If you dont want to show decimal when the result is a whole number then you need to check it like this:
Double res = (double)parseInt(first.getText()) / (double)parseInt(second.getText())
Integer x;
if(res % 1 == 0)
{
x = (int)res
}

Get the hundredths place value digit in a double variable using java

I am trying to get the digit in the hundredth's place of a double value (second digit after the decimal).
So, for example, if the double value is 48.4569999, I want to get the value 5 using any Math or BigDecimal methods, which is the hundredth's place after decimal.
I have tried the following code:
BigDecimal src = new BigDecimal("48.4569999");
BigDecimal a = src.remainder(BigDecimal.ONE);
System.out.println("a : " + a);
which results in .456999 but I dont want to use any String functions to get to the second digit. Is there any Math or Bigdecimal function to do this please ? Your help is appreciated.
Why not multiply by 100, to push your desired value to the right place, cast to int (to remove anything < 1) and then do % 10, which will give you the remainder (which will be your desired digit).
You may be looking for movePointRight(int).
int a = src.movePointRight(2).remainder(BigDecimal.TEN).intValue();
prints
a : 5

Java / Android - equation for rounding up

I am building an android application. In my app, i need to be able to round up a double (42.42 for example) and also get how much i added to the original number in order to round it up. My current code isn't working, and its outputting 0.. Anyway to fix this?
My current code:
float rounded = FloatMath.ceil(val);
double getDecimal = (val - FloatMath.floor(val))*100;
int noDecimal = (int) ((int) 100-getDecimal);
float toadd = (noDecimal/100);
In my code the "rounded" variable is the simpel rounding, and "toadd" should be how much i added to it. For some reason toadd always comes back as 0. Any help?
You're dividing noDecimal by 100. Both are ints, and the result will always be an int. In this case, it's an int between 0 and 1, which will always be truncated to 0.
What's wrong with just getting the number modulo 1 (%1), then getting the ceiling of the original number?
For completeness, you could simply change the last line to preserve the rest of the logic:
float toadd = noDecimal/100.0;
This changes the divisor to a float, and an int divided by a float yields a float.
float toadd = (noDecimal/100);
This will give you 0, as you are dividing smaller integer by larger one..
Try to do like this: -
float toadd = (Float.valueOf(noDecimal)/100);
Also, you don't need to do typecast twice in the below code: -
int noDecimal = (int) ((int) 100-getDecimal);
Just, outer cast is enough: -
int noDecimal = (int) (100-getDecimal);
Edit: - Also, you might want to use BigDecimal for this kind of problems..
Maybe I'm missing something or not getting your intention right, but if you just want to know what you added, why don't just use the difference?
float rounded = FloatMath.ceil(val);
float toadd = rounded-val;
Edit: As mentioned in the comments, this might not always give the absolutely accurate result. But it's the general idea which can be used with BigDecimal, which offers a higher precision.

Categories