如何将字符串转换为double ..请帮助 [英] How to convert string to double ..please help
问题描述
如何将字符串转换为双倍?
我有一个双变量dTot;
它的值是5.037717235E7
当我做FreemarkerTools.formatDecimal(dTot)时,我把它作为字符串50377172.35。
现在我想将它作为double值传递给方法格式(),这是如下所示。
当我做Double.parseDouble我得到dTot为5.037717235E7
CurrencyConverter.format(Double.parseDouble(Freema rkerTools.formatDecimal(dTot)));
>
公共类CurrencyConverter {
public static String format(double n){
if(n< 0)
return"(" + formatAcc(Math.abs(n))+")" ;;
返回formatAcc(n);
}
public static String formatAcc(double n){
// long decPart = 0;
String s1 ="" + n;
String [] nums = s1.split(" \\。");
long decPart = Integer.parseInt(nums [1]); < br $>
//长decPart =(长)((n-Math.floor(n))* 100);
if(n == 0)
return" 0.00";
int arr [] = new int [] {1000, 100,100,100,100,100};
String arrFromat [] = new String [] {" 000"," 00"," 00"," 00"," ; 00",
" 00" };
String s ="" ;;
for(int i = 0; i< arr.length; i ++){
long x =(long)(n%arr [i]);
if(s.length()> 0)
s ="," + s;
n = n / arr [i];
if(n< 1){
s = x + s;
休息;
}其他{
s =格式(x,arrFromat [i])+ s;
}
}
返回s +"。" + format(decPart," 00");
}
私有静态字符串格式(长n,字符串格式){
字符串temp = n +"" ;;
temp = format.substring(temp.length())+ temp;
返回temp;
}
public static void main(String [] args){
System.out.println(CurrencyConverter.format(554406 000.40));
}
}
How to convert string to double?
Ive got a double variable dTot;
its value is 5.037717235E7
when i did FreemarkerTools.formatDecimal(dTot) i got it as string "50377172.35".
now i want to pass it as a double value to the method format() which is shown below.
when i do Double.parseDouble i get dTot as 5.037717235E7
CurrencyConverter.format(Double.parseDouble(Freema rkerTools.formatDecimal(dTot)));
what else can i do to pass it as 50377172.35 to the method format()
public class CurrencyConverter {
public static String format(double n) {
if(n<0)
return "("+formatAcc(Math.abs(n))+")";
return formatAcc(n);
}
public static String formatAcc(double n){
//long decPart=0;
String s1 = ""+n;
String[] nums = s1.split("\\.");
long decPart = Integer.parseInt(nums[1]);
// long decPart = (long) ((n-Math.floor(n))*100);
if(n==0)
return "0.00";
int arr[] = new int[] { 1000, 100, 100, 100, 100, 100 };
String arrFromat[] = new String[] { "000", "00", "00", "00", "00",
"00" };
String s = "";
for (int i = 0; i < arr.length; i++) {
long x = (long) (n % arr[i]);
if (s.length() > 0)
s = "," + s;
n = n / arr[i];
if (n < 1) {
s = x + s;
break;
} else {
s = format(x, arrFromat[i]) + s;
}
}
return s+"."+format(decPart,"00");
}
private static String format(long n, String format) {
String temp = n + "";
temp = format.substring(temp.length()) + temp;
return temp;
}
public static void main(String[] args) {
System.out.println(CurrencyConverter.format(554406 000.40));
}
}
推荐答案
如何将字符串转换为双倍?
我有一个双变量dTot;
它的值是5.037717235E7
当我做了FreemarkerTools.formatDecimal(dTot)我得到它作为字符串" 503 77172.35"
现在我想将它作为double值传递给方法格式(),如下所示。
当我做Double时。 parseDouble我得到dTot为5.037717235E7
CurrencyConverter.format(Double.parseDouble(Freema rkerTools.formatDecimal(dTot)));
我还能做些什么将它作为50377172.35传递给方法格式()
>
公共类CurrencyConverter {
public static String format(double n){
if(n< 0)
return"(" + formatAcc(Math.abs(n))+")" ;;
返回formatAcc(n);
}
public static String formatAcc(double n){
// long decPart = 0;
String s1 ="" + n;
String [] nums = s1.split(" \\。");
long decPart = Integer.parseInt(nums [1]); < br $>
//长decPart =(长)((n-Math.floor(n))* 100);
if(n == 0)
return" 0.00";
int arr [] = new int [] {1000, 100,100,100,100,100};
String arrFromat [] = new String [] {" 000"," 00"," 00"," 00"," ; 00",
" 00" };
String s ="" ;;
for(int i = 0; i< arr.length; i ++){
long x =(long)(n%arr [i]);
if(s.length()> 0)
s ="," + s;
n = n / arr [i];
if(n< 1){
s = x + s;
休息;
}其他{
s =格式(x,arrFromat [i])+ s;
}
}
返回s +"。" + format(decPart," 00");
}
私有静态字符串格式(长n,字符串格式){
字符串temp = n +"" ;;
temp = format.substring(temp.length())+ temp;
返回temp;
}
public static void main(String [] args){
System.out.println(CurrencyConverter.format(554406 000.40));
}
}
How to convert string to double?
Ive got a double variable dTot;
its value is 5.037717235E7
when i did FreemarkerTools.formatDecimal(dTot) i got it as string "50377172.35".
now i want to pass it as a double value to the method format() which is shown below.
when i do Double.parseDouble i get dTot as 5.037717235E7
CurrencyConverter.format(Double.parseDouble(Freema rkerTools.formatDecimal(dTot)));
what else can i do to pass it as 50377172.35 to the method format()
public class CurrencyConverter {
public static String format(double n) {
if(n<0)
return "("+formatAcc(Math.abs(n))+")";
return formatAcc(n);
}
public static String formatAcc(double n){
//long decPart=0;
String s1 = ""+n;
String[] nums = s1.split("\\.");
long decPart = Integer.parseInt(nums[1]);
// long decPart = (long) ((n-Math.floor(n))*100);
if(n==0)
return "0.00";
int arr[] = new int[] { 1000, 100, 100, 100, 100, 100 };
String arrFromat[] = new String[] { "000", "00", "00", "00", "00",
"00" };
String s = "";
for (int i = 0; i < arr.length; i++) {
long x = (long) (n % arr[i]);
if (s.length() > 0)
s = "," + s;
n = n / arr[i];
if (n < 1) {
s = x + s;
break;
} else {
s = format(x, arrFromat[i]) + s;
}
}
return s+"."+format(decPart,"00");
}
private static String format(long n, String format) {
String temp = n + "";
temp = format.substring(temp.length()) + temp;
return temp;
}
public static void main(String[] args) {
System.out.println(CurrencyConverter.format(554406 000.40));
}
}
就像我之前已经指出的那样,改变你的方法
Like I already pointed out before, change your method
就像我之前已经指出的那样,改变你的方法
Like I already pointed out before, change your method
但我在格式()内部进行了大量的计算。
我如何用字符串进行计算?
请给出代码
But im doing a lot of calculations withg that double value inside format().
How do i do the calculations with string?
Please c the code given
在格式方法中你可以转换使用parseDouble返回你的double,这样你现在可以将你的值作为字符串和该方法中的double使用
Inside the format method you can then convert back to your double using parseDouble so that you now have your value both as a string and as a double inside that method
这篇关于如何将字符串转换为double ..请帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!