Get the App
SLTechnology News&Howtos  ›  Development  › 

How to convert decimal values to Long types using String types

Shulou Source: shulou.com Published: 2022-06-03 19:21:16 11月01日 Update

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!

Tags: Type numeric decimal article content integer time amount not much business most numbers solutions industry solutions information information channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia MariaDB MySQL Xiaomi Redmi