字符串转换的类,如230991.291==>230,991.291
2005-01-26 14:19
441 查看
public class FloattoString{
//if given 230991.291, should return the String "230,991.291"
//if given 200.0, should return the string "200"
//if given 87238.001, should return the string "87,238.001"
//if give -23870.0, should return the string "-23,870"
public String doubleToString(double value){
String strValue=value+"";
String strInt="";//取其整形部分
String strDec="";//取其小数部分
String result="";
//检查是否有小数点
int pos=strValue.lastIndexOf(".");
//if(pos>0){
//把字符按小数点拆分
strInt=strValue.substring(0, pos);
strDec=strValue.substring(pos);
int intValue=Integer.parseInt(strInt);
//加上小数部分
if(intValue!=value){
result+=strDec;
}
//如果是正数则直接输出,负数则加上负号
String resultTemp="";
if(intValue>=0){
resultTemp=getResult(intValue);
result=resultTemp+result;
}else{
intValue=intValue*-1;
resultTemp=getResult(intValue);
result="-"+resultTemp+result;
}
//如果小数点后为0则不按整数输出
//}
return result;
}
//把传过来的整弄值变成123456789--->123,456,789
private String getResult(int value){
int intValue=value;
String result="";
while(true){
int t1=intValue/1000;
int t2=intValue%1000;
if(t1!=0){
result=","+t2+result;
}
//最后一次前面不加","
if(t1==0){
result=t2+result;
break;
}
intValue=t1;
}
return result;
}
public static void main(String[] args){
//if given 230991.291, should return the String "230,991.291"
//if given 200.0, should return the string "200"
//if given 87238.001, should return the string "87,238.001"
//if give -23870.0, should return the string "-23,870"
double a=(double)230991.291;
double b=(double)200.0;
double c=(double)87238.001;
double d=(double)-23870.0;
double e=(double)1234567;
FloattoString fs=new FloattoString();
String rs=fs.doubleToString(a);
System.out.println(rs);
System.out.println(fs.doubleToString(b));
System.out.println(fs.doubleToString(c));
System.out.println(fs.doubleToString(d));
System.out.println(fs.doubleToString(e));
//System.out.println(123.0==123);
}
}
//if given 230991.291, should return the String "230,991.291"
//if given 200.0, should return the string "200"
//if given 87238.001, should return the string "87,238.001"
//if give -23870.0, should return the string "-23,870"
public String doubleToString(double value){
String strValue=value+"";
String strInt="";//取其整形部分
String strDec="";//取其小数部分
String result="";
//检查是否有小数点
int pos=strValue.lastIndexOf(".");
//if(pos>0){
//把字符按小数点拆分
strInt=strValue.substring(0, pos);
strDec=strValue.substring(pos);
int intValue=Integer.parseInt(strInt);
//加上小数部分
if(intValue!=value){
result+=strDec;
}
//如果是正数则直接输出,负数则加上负号
String resultTemp="";
if(intValue>=0){
resultTemp=getResult(intValue);
result=resultTemp+result;
}else{
intValue=intValue*-1;
resultTemp=getResult(intValue);
result="-"+resultTemp+result;
}
//如果小数点后为0则不按整数输出
//}
return result;
}
//把传过来的整弄值变成123456789--->123,456,789
private String getResult(int value){
int intValue=value;
String result="";
while(true){
int t1=intValue/1000;
int t2=intValue%1000;
if(t1!=0){
result=","+t2+result;
}
//最后一次前面不加","
if(t1==0){
result=t2+result;
break;
}
intValue=t1;
}
return result;
}
public static void main(String[] args){
//if given 230991.291, should return the String "230,991.291"
//if given 200.0, should return the string "200"
//if given 87238.001, should return the string "87,238.001"
//if give -23870.0, should return the string "-23,870"
double a=(double)230991.291;
double b=(double)200.0;
double c=(double)87238.001;
double d=(double)-23870.0;
double e=(double)1234567;
FloattoString fs=new FloattoString();
String rs=fs.doubleToString(a);
System.out.println(rs);
System.out.println(fs.doubleToString(b));
System.out.println(fs.doubleToString(c));
System.out.println(fs.doubleToString(d));
System.out.println(fs.doubleToString(e));
//System.out.println(123.0==123);
}
}
相关文章推荐
- 用string.Join与List<T>或者与string数组转换为字符串
- 【.Net码农】C# List<string>与String字符串相互转换
- vc中把字符串转换成16进制数的函数(如'7F'-&gt;0x7F)
- .NET将字符串转换成List<T>
- 字符串转换--str ip --.> num ip
- 字符串转换为整数”123“->123
- vc中把字符串转换成16进制数的函数(如^_^7f^_^-&gt;0x7f)
- perl 数字<->字符串及其进制间的转换
- 字符串编码之间的转换(GB2312<->UTF8<->Unicode)及URLEncoding .
- java字符串转义,把<>转换成<>等字符【原】
- VB 字节数组和字符串的转换问题 (String<>Byte)
- 问题1:java中没有实现这种“byte a = 0xB2 --> String b = “B2””转换的简单实现需要自己实现。 答:自己编写的转换函数,思路将byte的高低4位分开,分别转换为对应的字符然后合成返回的字符串。 java 代码 1.
- java 将jso 格式的字符串转换为List<Map<String,String>>
- java 后台使用gson 将json字符串转换为List<Map<String,object>> 数组
- 字符串编码之间的转换(GB2312<->UTF8<->Unicode)及URLEncoding
- 将输入的字符串(字符串仅包含小写字母‘a’到‘z’),按照如下规则,循环转换后输出:a->b,b->c,…,y->z,z->a;若输入的字符串连续出现两个字母相同时,后一个字母需要连续转换2次。例如:
- 将Map和List<Map>zhuan转换成xml字符串
- Timestamp 类型的时间转换成字符串
- Ilist<T>转换为List<T>
- Redis 存储字符串和对象<转>