Convert from Float to integer - java

I built a simple Android "accelerometer" Sensor Application.
This is The Java Code link.
The output is like this:
X:8.87654322
Y:0.564321
Z:4.0195783
And sometimes it goes longer ...
I want to convert the float to an integer or just have 2 numbers
(I'm a beginner In Java.)
Thanks

double y = 9.37923929732232;
int a = (int)y;

double d = 4.232
int answer = (int) Math.round(d);

i think you want to convert float array values of accelerometer to int below is the float array with two float values i am just rounding off the value of float element of oth index using Math.round(arrayname[indexnumber])
float[] fArray = new float[] {(float) 6574.748, (float) 789.999};
int f1 = Math.round(fArray[0]);
System.out.println(f1);
this will round off the float value for ex 6574.748 will be rounded to 6575

To convert float sensor data to integer you have to mutiply them, and then round.
e.g you want 1 digit after comma. 0.56432 -> 6
double y = 0.564321;
int yi = (int) Math.Round(y * 10); // for taking 2 digits after comma use 100
Then you have handy acceleration sensor data.

For getting two decimal places use (java.text.DecimalFormat:)
You can try this
double roundTwoDecimals(double d) {
DecimalFormat twoDForm = new DecimalFormat("#.##");
return Double.valueOf(twoDForm.format(d));
}
For integer there are other answers in the post.

Related

Convert int to float in Java without rounding

I have int value 40959999. How to convert it to float without rounding into 409599,99?
result of float f = 40959999/100.00f will be 409600.0
Use double instead of float
double d = 40959999/100.00f;
Double and Float have inherent imprecision that is impossible to avoid (more info on the why here). Using a double you may not have your number rounded in this scenario but not in all. If you work with something that should never be rounded (like prices), you should be using BigDecimal instead.
Implement your int value like shown. Rather than using float use BigDecimal instead.
BigInteger b1 = BigInteger.valueOf(40959999);
int scale = 2;
BigDecimal d1 = new BigDecimal(b1, scale);
System.out.println(d1);
run:
409599.99

Division and Modulo-Division of a double value to get integer value as the result [duplicate]

I need to cast a double to an int in Java, but the numerical value must always round down. i.e. 99.99999999 -> 99
Casting to an int implicitly drops any decimal. No need to call Math.floor() (assuming positive numbers)
Simply typecast with (int), e.g.:
System.out.println((int)(99.9999)); // Prints 99
This being said, it does have a different behavior from Math.floor which rounds towards negative infinity (#Chris Wong)
To cast a double to an int and have it be rounded to the nearest integer (i.e. unlike the typical (int)(1.8) and (int)(1.2), which will both "round down" towards 0 and return 1), simply add 0.5 to the double that you will typecast to an int.
For example, if we have
double a = 1.2;
double b = 1.8;
Then the following typecasting expressions for x and y and will return the rounded-down values (x = 1 and y = 1):
int x = (int)(a); // This equals (int)(1.2) --> 1
int y = (int)(b); // This equals (int)(1.8) --> 1
But by adding 0.5 to each, we will obtain the rounded-to-closest-integer result that we may desire in some cases (x = 1 and y = 2):
int x = (int)(a + 0.5); // This equals (int)(1.8) --> 1
int y = (int)(b + 0.5); // This equals (int)(2.3) --> 2
As a small note, this method also allows you to control the threshold at which the double is rounded up or down upon (int) typecasting.
(int)(a + 0.8);
to typecast. This will only round up to (int)a + 1 whenever the decimal values are greater than or equal to 0.2. That is, by adding 0.8 to the double immediately before typecasting, 10.15 and 10.03 will be rounded down to 10 upon (int) typecasting, but 10.23 and 10.7 will be rounded up to 11.
(int)99.99999
It will be 99.
Casting a double to an int does not round, it'll discard the fraction part.
Math.floor(n)
where n is a double. This'll actually return a double, it seems, so make sure that you typecast it after.
This works fine int i = (int) dbl;
new Double(99.9999).intValue()
try with this, This is simple
double x= 20.22889909008;
int a = (int) x;
this will return a=20
or try with this:-
Double x = 20.22889909008;
Integer a = x.intValue();
this will return a=20
or try with this:-
double x= 20.22889909008;
System.out.println("===="+(int)x);
this will return ===20
may be these code will help you.
Try using Math.floor.
In this question:
1.Casting double to integer is very easy task.
2.But it's not rounding double value to the nearest decimal. Therefore casting can be done like this:
double d=99.99999999;
int i=(int)d;
System.out.println(i);
and it will print 99, but rounding hasn't been done.
Thus for rounding we can use,
double d=99.99999999;
System.out.println( Math.round(d));
This will print the output of 100.

Possible loss of precision during conversion of float number

I have google on how to get 2 decimal for a float number in java. Below are my codes. Finally here float totalWeight = 0.1*levinWeight+0.8*lsmWeight; I get the error of possible loss of precision ? I would want to first covert the string into float and then have it to be 2 decimal places.
float levinWeight = Float.parseFloat(dataOnlyCombine[2]);
float lsmWeight = Float.parseFloat(dataOnlyCombine[3]);
DecimalFormat df = new DecimalFormat("#.##");
levinWeight = Float.valueOf(df.format(levinWeight));
lsmWeight = Float.valueOf(df.format(lsmWeight));
float totalWeight = 0.1*levinWeight+0.8*lsmWeight;
If you are concerned about precision
don't use float, it has the lowest precision of any option available. I suggest using double or BigDecimal
use operation which involve values which can be accurately represented. 0.1 * x will give you error because 0.1 cannot be represented precisely. Using x / 10.0 will have less error.
I would write something like this
double levinWeight = Double.parseDouble(dataOnlyCombine[2]);
double lsmWeight = Double.parseDouble(dataOnlyCombine[3]);
double totalWeight = (levinWeight + 8 * lsmWeight) / 10.0;
// perform rounding only at the end as appropriate.
// to round to two decimal places
double totalWeight2 = Math.round(totalWeight * 100) / 100.0;
float levinWeight = Float.parseFloat(dataOnlyCombine[2]);
float lsmWeight = Float.parseFloat(dataOnlyCombine[3]);
float totalWeight = 0.1*levinWeight+0.8*lsmWeight;
DecimalFormat df = new DecimalFormat("#.##");
String totalWeightValue = df.format(totalWeight);
If you really want to do it like that, then use BigDecimal. Those floating point classes are perfect for precision. Take a look at them:
http://voidexception.weebly.com/java-bigdecimal---dealing-with-high-precision-calculations.html
http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html
http://www.javaworld.com/article/2075315/core-java/make-cents-with-bigdecimal.html
Default IEEE 746 floating points will not suit your needs. Alternatively, you could use integers and thread them factor 100. So:
100 is equivalent to 1.00
452 is equivalent to 4.52
1 is equivalent to 0.01

How can I show up to 2 decimal places without rounding off?

I want to take two decimal places only for a float without rounding off. eg. 4.21777 should be 4.21 and not 4.22. How do I do this?
A simple answer:
double x = 4.21777;
double y = Math.floor(x * 100) / 100;
Subtract 0.005 and then round. For example if you just want to print the number you can use a format of %f6.2 and the value x-0.005.
float f = 4.21777 * 100;
int solution = (int)f;
f = solution/100;
This should work ;)
Explanation: By multiplying with 100, you will get 421.777, which, castet to int, is being rounded down to 421. Now divided by 100 returns its actual value.

how to change the double value as four decimal point value [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Round a double to 2 significant figures after decimal point
i've got lat and long point like this,
x1: 11.955165229802363
y1: 79.8232913017273
i need to convert 4 decimal point
x1 = 11.9552
y1 = 79.8233
Try
double roundTwoDecimals(double d)
{
DecimalFormat twoDForm = new DecimalFormat("#.####");
return Double.valueOf(twoDForm.format(d));
}
Math.ceil(x1* 10000) / 10000
Replace 10000 with 10^N, where N is number of digits after dot. In case of 4 digits after dot, precision shouldn't be lost.
Try this
String.format("%.4f", 11.955165229802363)
Assuming you want to round/truncate the decimal, and speed is not a large consideration, you want to use BigDecimal(BigInteger unscaledVal, int scale) with scale set to 4.
DecimalFormat dtime = new DecimalFormat("#.####");
^^^^
x1= Double.valueOf(dtime.format(x1));
float round = Round(num,4);
System.out.println("Rounded data: " + round);
}
public float Round(float Rval, int Rpl) {
float p = (float)Math.pow(10,Rpl);
Rval = Rval * p;
float tmp = Math.round(Rval);
return (float)tmp/p;
}
If you only want to display the value like that, use a DecimalFormat to convert the value to a string, then display that.
If you really want to round it to four digits, you can achieve that by multiplying by 10000, rounding, then dividing again. However, I would advise against it, since not all decimal numbers can be properly represented in floating point format. Changes are you'll just get something like you already had.
If you really want four digits to work with as an internal state, use a BigDecimal instead. It is properly equipped to do what you want.

Categories