How to convert decimal values to Long types using String types
Editor to share with you how to use String type decimal value to convert to Long type, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Decimal values of String type are converted to Long type
Values are divided into decimals and integers. When the type passed in is String and the type to be obtained is Long, this can be achieved directly through Long.valueOf (), but it will appear when the passed String type is worth the decimal value (for example, the amount value: "29.90". Note: the amount value is often multiplied by 100 and then the business logic is executed below:)
Exception java.lang.NumberFormatException: For input string: "29.90"
The solution is as follows
When the number of multi-decimal places is converted to long, it needs to be converted to integer transition first.
String orderAmount = "29.90"; Number num = Float.parseFloat (orderAmount) * 100; int oamount = num.intValue (); long _ cost = Long.valueOf (oamount); String type to int, longString str1 = "123"; String str2 = "123.0"
No decimal: can be directly converted to int
Int a = Integer.parseInt (str)
With decimal, converting directly to int will report a number formatting exception, which needs to be converted to double first, and then to int
Turn to intint b = (int) Double.parseDouble (str); turn to longlong c = (long) (Double.parseDouble (str)). The above is all the content of the article "how to convert decimal values of String types to Long types". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!