I am trying convert String value into Double.
But toDouble() automatically removing 0 from output value.
If String value is "1.30" then after conversion toDouble() returns 1.3.
val value = String.format(
"%02d.%02d",
TimeUnit.MILLISECONDS.toHours(milli) % 24,
TimeUnit.MILLISECONDS.toMinutes(milli) % 60
)
return value.toDouble()```
Input value : "1.30"
Retuned value : 1.3
Expected value : 1.30
Thanks in advance.
Related
I am summing double value from arraylist its giving additional decimals as 99999, how to fix this, please guide
ex
class ExDet{var expName:String ="",var expAmount:Double = 0.0}
val arrayList = ArrayList<ExDet>()
arrayList.add(ExDet("Abc 1",45.66))
arrayList.add(ExDet("DEF 1",10.0))
arrayList.add(ExDet("Lee 1",600.89))
arrayList.add(ExDet("Ifr 1",200.9))
var amt = arrayList.sumByDouble{ it.expAmount }
Expected Value of Amount is :
Amt = 857.45
But it returns
Amt = 857.4499999
Sample Code to Test
data class ExDet(var expName:String ="" ,var expAmount:Double=0.0)
fun main(args: Array<String>) {
val arrayList = ArrayList<ExDet>()
arrayList.add(ExDet("Abc 1",45.66))
arrayList.add(ExDet("DEF 1",10.0))
arrayList.add(ExDet("Lee 1",600.89))
arrayList.add(ExDet("Ifr 1",200.9))
var amt = arrayList.sumByDouble{ it.expAmount }
println("Amount is : $amt")
}
The issue you are confronted with is that floating point numbers are build on top of base 2, not base 10.
Think how you can easily represent a third as a fraction (1/3), but when you convert to decimal you get a repeating (recurring) number after the radix point (i.e. 0.33...). Some decimal numbers are recurring when represented in base-2, e.g. x.9. The computer has a finite number of bits, so the (base-2) number is truncated. All the truncation errors can add up.
You need to round to the required precision (e.g. round(x * 100) / 100).
If you are only interested in how it is displayed then you can use the format function with something like "%.2f".
String.format("%.2f", value)
I am converting 4 integers into binary and everything works fine until it has to convert a zero.
for example:
int subnet1 = 255;
int subnet2 = 255;
int subnet3 = 255;
int subnet4 = 0;
binarystring = Integer.toBinaryString(subnet1)
+ Integer.toBinaryString(subnet2)
+ Integer.toBinaryString(subnet3)
+ Integer.toBinaryString(subnet4);
BinaryView.setText(binarystring);
text would look like this: 11111111 11111111 11111111 0 (without the space inbetween)
why wont it convert the 0 to 00000000 ??
Because value for "0000000" is same for "0" that's why it set as "0".
If you want to print that result. Just make a string of "0000000" for printing. Because int value always convert it.
Why? By design. From the documentation:
This value is converted to a string of ASCII digits in binary (base 2) with no extra leading 0s.
(Emphasis added.)
If you want 8 characters, append the result to a string of 7 zeros, and take the last 8 characters. In pseudo code:
intermediateString = "0000000" + Integer.toBinaryString( 0 ); // obviously don't hard code zero; just for example
finalString = intermediateString.substring( intermediateString.length() - 8);
If you read the documentation here: https://developer.android.com/reference/java/lang/Integer.html#toBinaryString(int)
It tells you:
The unsigned integer value is the argument plus 232 if the argument is negative; otherwise it is equal to the argument. This value is converted to a string of ASCII digits in binary (base 2) with no extra leading 0s.
Example :
value = 0.0
I want to covert it to
Ans :value = 00.0
example
value = 12.0
Ans: 12.0
example
value = 1.0
Ans: 01.0
Right now for float values i am using String.format( " %.2f" ,variable_value)
is there any way like this to convert values
Thanks
you can use string.format method like below:
String value= "_%02d" + "_%s";
it converts numbers like :
String.format("%02d", 1); // => "01"
you can see forrmatter doc for other modifiers
i from API I receive next string:
String userId = "4463570100035744";
I need to convert it to int, so I tried next code:
try {
int id = Integer.parseInt(userId);
} catch (NumberFormatException e){
e.printStackTrace();
}
but I still catch exeption....
what can be the reason?
REASON: value is outside the range of an int
ACTION: You must use Long and not Integer .Use a long/Long instead.
Integer.MAX_VALUE = 2147483647
Integer.MIN_VALUE = -2147483648
Long.MAX_VALUE = 9223372036854775807
Long.MIN_VALUE = -9223372036854775808
4463570100035744 is too large a number for an Int32 variable. You could consider using a long variable type.
you take variable userId as String and you put integer value in it and then you parse it to convert in integer. Your mistake is you put integer value in String variable userId.
You need to put it like this :
String userId = "4463570100035744";
One more thing keep the size of variable in mind. I think value is too large then the size of int.
And now you edited your question, after people post answers to your actual problem.
You can refer to the docs for the Java primitive types to select the appropriate type for your variable: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
The primitive types available to store a number like your userId are:
byte (1 byte) Range: −128 to 127
short (2 bytes) Range: −32768 to 32767
int (4 bytes) Range: −2,147,483,648 to 2,147,483,647
float (4 bytes) Range: 3.4e−038 to 3.4e+038
long (8 bytes) Range: 9,223,372,036,854,775,808 to 9,223,372,036,854,755,807
double (8 bytes) Range: 1.7e−308 to 1.7e+038
Notice how your int is 4463570100035744 which compared to int has a difference of 4,463,567,952,552,097.
Your id variable would be best suited for a long.
try
{
long id = Long.parseLong(userId);
System.out.println("long id = " + id);
}
catch (NumberFormatException nfe)
{
System.out.println("NumberFormatException: " + nfe.getMessage());
}
My application does some basic arithmetic processes and then adds them to a TextView. Because I want them the result be shown up to XX,XX I format my string with %.2f. Now, when I try to retrieve this result and use it in another arithmetic process, it gives me an error of:
java.lang.NumberFormatException: Invalid double: "8,86" (or any number for that matter)
How can I make the second process convert the string from the TextViewwithout getting an error?
process 1
int newProductQuantity = Integer.valueOf(productQuantity.getText().toString());
double newProductPrice = Double.valueOf(productPrice.getText().toString());
double newProductVAT = Double.valueOf(productVat.getText().toString());
double newProductPriceSum = ((newProductPrice + (newProductPrice * (newProductVAT / 100))) * newProductQuantity);
String newProductPriceSumTexta = String.format("%.2f", newProductPriceSum);
productPriceSum.setText(newProductPriceSumTexta);
process 2
double newOrderFinalLastSum = Double.parseDouble(newOrderFinalSum.getText().toString());
double newOrderFinalNewSum = Double.parseDouble(productPriceSum.getText().toString());
double newOrderFinalOmegaSum = newOrderFinalLastSum + newOrderFinalNewSum; //error is here
String newOrderFinalOmegaSumText = String.format("%.2f", newOrderFinalOmegaSum);
newOrderFinalSum.setText(newOrderFinalOmegaSumText);
your issue is Locale related. If you want always a dot . as separator, you should specify a Locale that use it. You can use format method that takes as first parameter a Locale object. For instance
String.format(Locale.UK,...
From the documentation of public static String format(Locale l, String format, Object... args)
Returns a formatted string using the specified locale, format string,
and arguments.
where
l - The locale to apply during formatting. If l is null then no
localization is applied.