469,317 Members | 1,963 Online 58
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);

// 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));
}
}
Mar 13 '07 #1
15 7186 r035198x
13,262 8TB
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);

// 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));
}
}

Expand|Select|Wrap|Line Numbers
1.  public static String format(double n) {
to
Expand|Select|Wrap|Line Numbers
1.  public static String format(String n) {
Everything will become much easier then.
Mar 13 '07 #2
shiniskumar
58 Expand|Select|Wrap|Line Numbers
1.  public static String format(double n) {
to
Expand|Select|Wrap|Line Numbers
1.  public static String format(String n) {
Everything will become much easier then.
But im doing a lot of calculations withg that double value inside format().
How do i do the calculations with string?

Mar 13 '07 #3
r035198x
13,262 8TB
But im doing a lot of calculations withg that double value inside format().
How do i do the calculations with string?

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

Expand|Select|Wrap|Line Numbers
1.  format(String s) {
2.     double doubleValue = Double.parseDouble(s);
3.     //If strin value is required, use s
4.     //If double value is required, use doubleValue
5.     //...
6.
Mar 13 '07 #4
shiniskumar
58 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

Expand|Select|Wrap|Line Numbers
1.  format(String s) {
2.     double doubleValue = Double.parseDouble(s);
3.     //If strin value is required, use s
4.     //If double value is required, use doubleValue
5.     //...
6.
Still i have the same problem.
When i get a string in format() as "50377172.35"
when parsing it i get 5.037717235E7
in the method format(), im calling another method formatAcc() where im splitting the number into 2 and taking Integer.parseInt() of the decimal part.
Trying to parse it, i get NumberFormatException...

Mar 13 '07 #5
r035198x
13,262 8TB
Still i have the same problem.
When i get a string in format() as "50377172.35"
when parsing it i get 5.037717235E7
in the method format(), im calling another method formatAcc() where im splitting the number into 2 and taking Integer.parseInt() of the decimal part.
Trying to parse it, i get NumberFormatException...

Look at your design again formatAcc() is the one that needs to split the value right? So formatAcc() needs the String value so it can use the split method on that value.
Mar 13 '07 #6
shiniskumar
58 Look at your design again formatAcc() is the one that needs to split the value right? So formatAcc() needs the String value so it can use the split method on that value.
This is the formatAcc() method.
public static String formatAcc(double n){
//long decPart=0;
String s1 = ""+n;
String[] nums = s1.split("\\.");
long decPart = Integer.parseInt(nums);

// 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");
}
Even if i pass the number n as string, im doing calculations inside this function, not only splitting. and if i make it as string and convert it to double, i get exceptions
Mar 13 '07 #7
r035198x
13,262 8TB
This is the formatAcc() method.
public static String formatAcc(double n){
//long decPart=0;
String s1 = ""+n;
String[] nums = s1.split("\\.");
long decPart = Integer.parseInt(nums);

// 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");
}
Even if i pass the number n as string, im doing calculations inside this function, not only splitting. and if i make it as string and convert it to double, i get exceptions
Like I said previously, give it a String. Then you create another variable, the double, that use for calculations that require the double. Just pass Strings to all your functions and where a double is required use parseDouble.
Mar 13 '07 #8
shiniskumar
58 Like I said previously, give it a String. Then you create another variable, the double, that use for calculations that require the double. Just pass Strings to all your functions and where a double is required use parseDouble.
That is where the problem is. i pass the arguments using String, then convert it into double for calculations. it works well for small numbers. But for big numbers, using parseDouble gives the exponential value. so i cant get the correct double value for calculations.

ie if my string is "55440600.40" using parseDouble i get 5.544060040E7
how can i use this value for calculations.?
Mar 13 '07 #9
r035198x
13,262 8TB
That is where the problem is. i pass the arguments using String, then convert it into double for calculations. it works well for small numbers. But for big numbers, using parseDouble gives the exponential value. so i cant get the correct double value for calculations.

ie if my string is "55440600.40" using parseDouble i get 5.544060040E7
how can i use this value for calculations.?

Use it like that for calculations. You will get the correct result.

Which calculation are you getting problems with it?
Mar 13 '07 #10
shiniskumar
58 Use it like that for calculations. You will get the correct result.

Which calculation are you getting problems with it?
after splitting i get the decimal part as 355434664e7
when taking parseInt() of this i get error
long decPart = Integer.parseInt(nums);
Mar 13 '07 #11
r035198x
13,262 8TB
after splitting i get the decimal part as 355434664e7
when taking parseInt() of this i get error
long decPart = Integer.parseInt(nums);
Do not split the double, split the String.

Now look here my friend.

You have your number as a string as "55440600.40".
Pass it to the function like that as a String. So your function becomes

Expand|Select|Wrap|Line Numbers
1.  public void myFunction(String value) {
2.     //Now split the String value
3.     String[] values = value.split("\\.");
4.     //Then do your parseLong or parseInt on the values of the array
5.     //If you need to do calculations using the value, then change it to
6.     //double using parseDouble
7.
Now where do you not understand?
Mar 13 '07 #12
shiniskumar
58 Do not split the double, split the String.

Now look here my friend.

You have your number as a string as "55440600.40".
Pass it to the function like that as a String. So your function becomes

Expand|Select|Wrap|Line Numbers
1.  public void myFunction(String value) {
2.     //Now split the String value
3.     String[] values = value.split("\\.");
4.     //Then do your parseLong or parseInt on the values of the array
5.     //If you need to do calculations using the value, then change it to
6.     //double using parseDouble
7.
Now where do you not understand?

got it correctly when i did parseLong instead of parseInt.

Thankyou very much
Mar 13 '07 #13
r035198x
13,262 8TB
got it correctly when i did parseLong instead of parseInt.

Thankyou very much
Great! I hope everything is now working fine.
Mar 13 '07 #14
misguidedcoder
2 how to handle "ü" character is jsp???

can neone help...urgent help required
Mar 13 '07 #15
r035198x
13,262 8TB
how to handle "ü" character is jsp???

can neone help...urgent help required