当前位置:网站首页>Error reason for converting string to long type: to convert to long type, it must be int, double, float type [easy to understand]
Error reason for converting string to long type: to convert to long type, it must be int, double, float type [easy to understand]
2022-07-28 14:42:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
The solution to this problem is one :
String strTime = "12.5416132";
double dblTime = Double.parseDouble(strTime);
long lngTime = new Double(dblTime).longValue();
log.info("lngTime:"+lngTime);Please see the following examples and explanations for the specific reasons for the error report :
The above example :
string a =100.1;
long b=long.parse(a);
Conversion times “ The input string is not in the correct format ”
When a =100.0 when
long b=long.parse(a,NumberStyles.AllowDecimalPoint);
It can be converted normally ,
and a=100.9 when
long b=long.parse(a,NumberStyles.AllowDecimalPoint);
switch views , Report exceeding Int64 Maximum or minimum of ,Explanation of the reason :
Question 1 :long It's plastic surgery, that is Int64 type ,parse The object of must be an integer
Question two :
When a =100.0 when
long b=long.parse(a,NumberStyles.AllowDecimalPoint);
This is because of the use of NumberStyles.AllowDecimalPoint
Indicates that the numeric string can have a decimal point . If NumberStyles Values include AllowCurrencySymbol sign , The analyzed string includes currency symbols , Then the decimal separator character is composed of NumberFormatInfo. CurrencyDecimalSeparator Attribute determination . otherwise , The decimal separator is composed of NumberFormatInfo. NumberDecimalSeparator Attribute to determine .
Actually, decimal points are allowed , But as a long, Still can't parse Non integer , At this time a yes 100.00, You can discard the decimal point as an integer . in other words long stay parse By default a Treat as 100 Come on parse Of .
Question 3 :
At this time a become 100.1, The same is used NumberStyles.AllowDecimalPoint, But this time long the parse Is no longer an integer , So it will raise exceptions , exceed int64 That is to say long The scope of the , For this mistake , You can put it first. a=100.1 Make a transformation into shaping , Or more widely used double or decimal Come on parse, Such as decimal.Parse("100.1", NumberStyles.AllowDecimalPoint)Examples and explanations come from Click to open the link
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/128601.html Link to the original text :https://javaforall.cn
边栏推荐
- 聊天室功能的实现
- C# 获取当前路径7种方法
- QQ robot configuration record based on nonebot2
- linux安装mysql
- Recommended super easy-to-use mobile screen recording software
- C语言中浮点数据类型(你学废了吗)
- Floating point data type in C language (did you learn to waste it)
- 如何在 Core Data 中进行批量操作
- linux安装redis
- 8、 Picker usage drop-down box selection effect
猜你喜欢

How to effectively conduct the review meeting (Part 1)?

linux安装mysql

Recommended super easy-to-use mobile screen recording software
Excel VBA 开发过程中遇到的一些问题,解决方案,持续更新

linux安装redis

It's so hot that solar power can't take off? Hello, head

Hcip day 11

Focus on differentiated product design, intelligent technology efficiency improvement and literacy education around new citizen Finance

9、 Uni popup usage popup effect at the bottom of the drop-down box

58子站安居,经纪人营销管理平台登录接口加密逆向
随机推荐
Some problems encountered in the development of Excel VBA, solutions, and continuous updates
Super resolution reconstruction based on deep learning
【七夕】七夕孤寡小青蛙究极版?七夕节最终章!
Analysis of thrift serialization protocol
Career planning of Software Test Engineer
九、uni-popup用法 下拉框底部弹窗效果
多线程顺序运行有几种方法?
Raspberry pie foundation | summarize and record some operations in the learning process of raspberry pie
C# 读取ini文件、键值对操作
ScottPlot入门教程:获取和显示鼠标处的数值
Thrift 序列化协议浅析
10、 Timestamp
How to reduce the resolution of only 3D camera but not UI camera
Leetcode 0142. circular linked list II
2022高处安装、维护、拆除考试题库及在线模拟考试
9、 Uni popup usage popup effect at the bottom of the drop-down box
Iterator iterator interface
js的实例化方式
8、 Picker usage drop-down box selection effect
Excel VBA 免密查看VBE加密代码