问题:
给一个字符串,比如“-12.05”,把它转成相应的double类型的数。
分析:
在进行转换的时候,要注意以下问题:
1. 该字符串是否为空
2. 是否该字符串含有符号;
3. 该字符串内是否有非法字符;
4. 小数点的位置;
5. 该数是否越界;
代码如下:
public static double atod(String str) throws Exception {
boolean negative = false;
//get the value before the "."
double valueBeforeDot = 0.0d;
//get the value after the ".";
double valueAfterDot = 0.0d;
boolean pointAppear = false;
int count = 0;
//null or empty string
if (str == null || str.equals("")) {
throw new Exception("null string or the string has no character!");
}
for (int i = 0; i < str.length(); i++) {
//check whether the first character is "+" or "-"
if (i == 0 && (str.charAt(0) == '-' || str.charAt(0) == '+')) {
if (str.charAt(0) == '-') {
negative = true;
}
} else {
//check whether the character is "." and appears for
//the first time and appears at the correct position.
if (pointAppear == false && i != 0 && str.charAt(i) == '.'
&& (str.charAt(0) != '-' || str.charAt(0) != '+')) {
pointAppear = true;
} else {
if (str.charAt(i) >= '0' && '9' >= str.charAt(i)) {
if (pointAppear == false) {
valueBeforeDot = valueBeforeDot * 10 + (str.charAt(i) - '0');
if (valueBeforeDot > Double.MAX_VALUE) {
throw new Exception("out of Double range");
}
} else {
valueAfterDot = valueAfterDot * 10 + (str.charAt(i) - '0');
count++;
}
} else {
throw new NumberFormatException("not a double");
}
}
}
}
valueBeforeDot = valueBeforeDot + valueAfterDot /Math.pow(10, count);
return negative == true ? valueBeforeDot * -1 : valueBeforeDot;
}
扩展:
把一个字符串转成一个整数。 解答可以参考http://blog.csdn.net/beiyeqingteng/article/details/7000034
转载请注明出处:http://blog.csdn.net/beiyeqingteng
分享到:
相关推荐
您可能感兴趣的文章:sql语句中如何将datetime格式的日期转换为yy-mm-dd格式将WMI中的DateTime类型转换成VBS时间的函数代码LINQ字符串向datetime 转换时失败的处理方法Sql中将datetime转换成字符串的
java\String类型转换成整数double
}这种方法 支持多个字符串参数方法 返回值目前测试可支持String boolean double int float 引用方式字符串默认设定为 不带参数: 包名 类名@方法名@ 带参数:包名 类名@方法名@参数1:参数2:参数3 参数N 方式">本类...
Ubuntu 14.04 + QT 计算器开发实例1-----全局变量的使用以及double转字符串类型,具体使用详见我的博客。
//将double类型转为_int64,防止精度1丢失 //将double类型转为_int64,防止精度1丢失
Convertor按照IEEE 754标准对Float和Double类型进行转换
一些初学JAVA的朋友可能会遇到JAVA的数据类型之间转换的苦恼,例如,整数和float,double型之间的转换,整数和String类型之间的转换,以及处理、显示时间方面的问下面笔者就开发中的一些体会介绍给大家。 我们知道,...
将浮点数转为字符串的具体算法在网上少有涉及,一般都采用浮点法,即通过浮点运算确定需要的每一位上的数字。本文介绍的方法是定点法,即对IEEE 745[1]的浮点数编码规范进行硬解码。这种方法效率不高,但是精度确很...
decimal 模块实现了定点和浮点算术运算符,使用的是大多数人所熟悉的模型,而不是程序员熟悉的模型,即大多数计算机硬件实现的 IEEE 浮点数运算。这篇文章主要介绍了python里的decimal类型转换,需要的朋友可以参考下
使用str2num、str2double、str2int等函数来实现字符串转换为数字类型的功能
同时具有一个将时间转为字符串的函数,方便调用。 同时包含一个CHDateTime类,便于使用。 C++中的std::string功能强大,一般在可以情况的都使用string; MFC中的CString功能更为强大,如果string实现不了部分,使用...
各种数字类型转换成字符串型: String s = String.valueOf( value); // 其中 value 为任意一种数字类型。 字符串型转换成各种数字类型: String s = "169"; byte b = Byte.parseByte( s ); short t = Short....
对string作了一些扩展,包括string转化为int、string转化为double、string转化为bool、打印系统当前时间。但没有解决数据溢出的问题,请大神帮忙解决!
java实现任意进制的转换,包括2进制转16进制,10进制转16进制,10进制转任意进制,36进制加法,可自行手动设置转换进制和需要转换的数字.
16位转浮点型float,MODBUS 32位转浮点型float 64位转双浮点型double
将字符串中的所有非Double类型的字符全部替换成空格 以’#’开头直至行尾的内容被清空 返回一维numpy.array数组 import numpy import scipy def str2num(LineString,comment='#'): from io import StringIO as ...
matlab数组字符串 非double型数值-整型数据类型 算法开发、数据可视化、数据分析以及数值计算 Matlab课程 教程 进阶 资源
matlab数组字符串 非double型数值-单精度浮点型数据类型 算法开发、数据可视化、数据分析以及数值计算 Matlab课程 教程 进阶 资源
浮点数在内存中的存储机制和整型数...1、将字符串转换为float、double过程存在精度损失,只是float、double各自损失的精度不相同而已std::string str=”8.2″;float cc=atof(str.c_str()); //cc的值为8.1999998std:
double 把字符串变成ASCII 码值 eval 执行串形式的MATLAB 表达式 17.2 字符串查询(String tests) iscellstr 若是字符串组成的元胞数组则为真 ischar 若是字符串则为真 isletter 串中是字母则为真 isspace 串中是空格...