Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to convert decimal values to Long types using String types

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report