Getting improper data while perform devide operation in android - android

I am doing as below :
var value = 0.0
value = ((3300/1000).toDouble())
But getting value as 3.0 instead of 3.3
Why ?
Where am I doing mistake ? What I have to do to get the value as 3.3 ?

you are dividing integers and then converting them to a double afterwards, which doesn't work like you want it to.
3300/1000
these are integers, so no decimal values. 3300/1000 is just 3
3300.0/1000.0
these are doubles, so they have decimal values

Related

Android resources different value when pass to the function

Can anyone explain why the value of a android resource id (R.id.navigation_news) before and after passing to a function is different?
Before pass to function
fun showTabFragment() {
navigateWithStartDest(R.id.navigation_news) // R.id.navigation_news == -1000386
}
After
fun navigateWithStartDest(resId:Int){
//resId == 2131362119
//resId != R.id.navigation_news - true
...
}
Probably you get integer overflow. The reason is your integer is too big.
In computer programming, an integer overflow occurs when an arithmetic operation attempts to create a numeric value that is outside of the range that can be represented with a given number of digits – either larger than the maximum or lower than the minimum representable value.
See the link:
https://en.wikipedia.org/wiki/Integer_overflow
The solution is not operate on R.id values, but only pass them as resources.
It could occur because of Java memory model. In kotlin language all primitives turned into objects. So in your function you receive not exact value but reference for it.

Android: BigDecimal to Float loss of data

I store a variable in sqlite db as REAL but in my app I need to use it as BigDecimal.
The problem is, I am not able to save my BigDecimal variable to my sqlite without converting it to float or double.
so I do the following:
bigDecimal.floatValue();
but by doing that a loss of precision occurs.
for example
if I put to my editText from which I get the variable: 123456789
after converting it from bigDecimal to float I get : 123456792
The same happens if I try to cast bigdecimal to string:
String.valueOf(bigDecimal);
the problem occurs ONLY if the variable which I put is at least 8 digits long.
It behaves the same way if its a decimal number and the numbers differ always after the seven digit.
is that a normal behaviour ? how to avoid it?

why is minimum value from stringArray using colections.min() not working?

I've been using this method to find the minimum value of doubleArray :
colections.min()
but when i'm using this method to string Array, it got the wrong result. Likes this :
My stringArray value : {2.3 Km, 10,0 Km, 4.0 Km, 24,7 Km}
but when i call the result, its show 10.0 Km as minimum value instead 2,3 Km.
Can anybody tell me what is the perfect method that i can use to get the minimum value of the srtringArray ?
I know this is a very basic question, but i really need more explanation.
Thank you
A string beginning with 1 comes before a string beginning with 2. Just as it would with A and B. The minimum value of a string array is the string which comes first alphabetically. What are you expecting?

Floating Point is not correct

I am getting a value from server that is not containing any Floating point let say its
1234 and have to cvonvert it in Floating value with 2 decimal point like 12.34.
Right now what i am doing is getting value storing it in float that convert the current value 1234 to 1234.0
after that doing this
tempB=Math.floor(tempB)/100.0;
DecimalFormat df = new DecimalFormat("###.##");
RewardsBalance=df.format(tempB);
But with this i m having an issue that when i have value such that 1230 it results in 12.3 not that 12.30
but when i have value 1234 it gives the desired result that is 12.34
so what step i m missing any clue
12.3 and 12.30 are the same value. The problem is not the value but the code that incorrectly converts the right value to the wrong representation. You probably want "###.00". With "#", zero shows as absent.
Use this it will work
Two digits after point
Try this :
String.format("%.2f", your_value);
It will do just like you want
try this
tempB=Math.floor(tempB)/100.0;
DecimalFormat df = new DecimalFormat("0.00");
df.format(tempB);
it will work fine.

storing double values in SQLite: how to ensure precision?

i have a problem with double values i need to store in an android homed sqlite database. since these double values represent gps values (lat & lng), i really NEED an absolute precision down to the 9th number after the comma.
now i have a table like this:
CREATE TABLE x REAL lng;
and insert sth (hardcoded) like:
INSERT INTO x lng = '1.0';
and when reading lng from this table into some (java) double variable, i get a value like "0.999956837" - this renders the values pretty useless to me.
is there a way to enforce the precision i need other than storing the values as "text" fields (what would make expensive casts neccessary) or storing them as integers (meaning i need to multiply/divide at each write/read-op)?
SQLite is typeless, that means all representation is written as text, probably the wrapper api does some converts you don't know of, that you get those results.
If you need to store the data as string do it.
Just when you read out the double make sure you saved in the right format, you can use getDouble on the column.
double has about 17 decimal digits of precision, so if 9 digits is what you need, there should be no problem (assuming that you don't do any complex calculations on those values). Just make sure you never end up using float, because that has only about 7 digits of precision.
You should also make sure you understand how binary floating-point works, and that it will always result in seemingly "round" values becoming slightly off - which simply does not matter for most applications (including yours) as long as it happes somewhere in the 17th decimal digit. See that link also for alternatives for applications where it does matter.

Categories

Resources