Showing a percentage between 2 integers - android

I have a quiz game and i wanna show the percentage of level and maxlevel.
(levelvideo) is my first integer of current level
(QuestionLibraryVideo.mChoices.length) is my max level from length of table..
I try to show this = (levelvideo/QuestionLibraryVideo.mChoices.length)*100
but shows me only zero.
thsekato2.setText(Integer.toString((levelvideo/QuestionLibraryVideo.mChoices.length)*100));

IF you divide integer by integer, you will loose decimals after ,.
For example 3/2 is 1, beacuse it's 1,5, so integer have only 1.
Use float or double instead.
float f1 = levelvideo;
float f2 = QuestionLibraryVideo.mChoices.length;
zero.thsekato2.setText(Integer.toString((int)((f1/f2)*100)));

If you want to do this only with integer arithmetic, you can multiply the numerator by 100 before dividing. It doesn't have the precision offered by converting to a double, but then, it doesn't have the overhead of type casting either. This would look something like:
thsekato2.setText(Integer.toString((100*levelvideo)/QuestionLibraryVideo.mChoices.length));

Related

How can i show the value before the "." sign?

I'm creating a currency application but some of values are like "194.23564" or "1187.7594" so i want to show the user before the "." sign values. How can i make this with Kotlin ?
There is no need for data type conversion before the extraction of the integer part.
You can use substringBefore():
val number = "194.23564"
val intPart = number.substringBefore(".")
If you want the result as an integer number you can use now toIntOrNull(), instead of toInt(), so to avoid an exception in case the initial string has no integer part (like ".015"):
val intPart = number.substringBefore(".").toIntOrNull()
Other than suggested, I would not convert to Float. This is susceptible to rounding errors and may not return the value before the decimal point.
Example:
val num = "0.99999999"
println(num.toFloat().toInt()) // gives 1
Instead, split the string at the decimal point:
val num = "0.99999999"
val split = num.split('.')
println(split[0]) // gives 0
A nice side effect of this implementation is that it even works for integral numbers without a decimal point. If you need the result as an Int, simply call split[0].toInt().
There is no need to use float in this case. If you want to get the value before ".", you need to use int instead of float. When you use float you will get value in points, but when you use int, you will get the value before ","

how to increase float without strange number

so i tried to increase number with float, but sometime it comes strange number but i just want a number like 0.1, 0.2, until 5.0 but it sometime comes with strange number like 0.9000001
this is my code to increase the float
addRattingBtn.onClick {
quantity += 0.1f
if (quantity >= 5.0f) {
toast("maximum")
return#onClick
}
totalRatting(quantity.toBigDecimal())
}
i already convert it to BigDecimal but it still not work.
why this is happen? and how to make it right. please help
This is because the finite nonzero values of any floating-point value set can all be expressed in the form s · m · 2(e - N + 1), where s is +1 or -1, m is a positive integer less than 2N, and e is an integer between Emin = -(2K-1-2) and Emax = 2K-1-1, inclusive, and where N and K are parameters that depend on the value set. Hence, float values cannot accurately represent base 10 real numbers and as you go on adding them they loose the precision sometime and shows such results, you should be using big decimal for the precision.
For example:
BigDecimal number = new BigDecimal(0.1);
BigDecimal add = number.add(new BigDecimal(0.1));

which datat type should use for exact calculation in android

Multiplication of two float numbers provide value like below
130.82(float) X 62.0 (float) = 8110.8403 (float) instead of 8110.84 .
Also, 500001.0 (float) X 47.0 (float) = 2.3500048E7 (double) instead of 23500047
If it is about monetary calculation then you can try to use BigDecimal, int or long.
Floats and double both cannot be used for exact calculations. Both the datatypes follow the (IEEE 754) standards.
Shouldn't be so hard to figure out that all calculated values have a length of 8 unique numbers, followed by an exponential value ;)
to make sure your values will be any different from default length, you'd have to floor or max the values yourself or i.e. use round($value, $precision (length behind decimal dot)) in the example of php
Else, ofcourse, the value will retain the default length.

How to convert an integer value into floating point in java?

I am getting an integer value in my android application.I want to convert it into floating point number which is in this format
"0.xyF"
.I tried lot of methods.I know its simple but i am confused.Please help.
I am passing a value from one activity to another using putExtra.So in the second activity i have to convert it to float for setting the value as verticalMargin for my dialog window.I used this line for getting the value in second activity.
int data = getIntent().getIntExtra("value", 7);
This is used for setting the vertical margin.
wlp.verticalMargin = "the converted floating point number";
If i is the integer value, then try:
float f=i;
while(f>=1.0f)
f/=10.0f;
You question still isn't clear. If you're asking how to convert an integer value that represents a percentage from 0 to 100 into a floating point value, then it would be fpVal = intVal / 100.0;
If you just want a simple conversion of an integer into a floating point number with the same exact value (e.g. 7 --> 7.0), then you can just cast it: fpVal = (float) intVal;
In your first activity store your integer value in string like this
String margin = "0."+int_value;
then pass this string to second activity.
In the second activity get that string from extra and convert it to float.
float float_value = Float.parseFloat(margin);

how adjust double value to approximate value after

i am getting double values,i need to reduce to approximative value after . for example
i have 123.678,i need to format this as 124.
if i have 123.212 i need to format this as 123
how can i done this,can any one please help me.
Thank u in advance.
Sounds like you want Math.round() for floats or Math.round() for doubles
public static int round(float a)
public static long round(double a)
Returns the closest int/long to the
argument.
The result is rounded to an
integer by adding 1/2, taking the
floor of the result, and casting the
result to type int/long.
In other words,
the result is equal to the value of
the expression: (int)Math.floor(a + 0.5f) or (long)Math.floor(a + 0.5d)

Categories

Resources