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) ((nMath.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));
}
}
15 7186
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) ((nMath.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  public static String format(double n) {
to  public static String format(String n) {
Everything will become much easier then.
Like I already pointed out before, change your method  public static String format(double n) {
to  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?
Please c the code given
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
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  format(String s) {

double doubleValue = Double.parseDouble(s);

//If strin value is required, use s

//If double value is required, use doubleValue

//...

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  format(String s) {

double doubleValue = Double.parseDouble(s);

//If strin value is required, use s

//If double value is required, use doubleValue

//...

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...
Please help me recover frm this
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...
Please help me recover frm this
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.
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[1]);
// long decPart = (long) ((nMath.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
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[1]);
// long decPart = (long) ((nMath.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.
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.?
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?
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[1]);
after splitting i get the decimal part as 355434664e7
when taking parseInt() of this i get error
long decPart = Integer.parseInt(nums[1]);
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  public void myFunction(String value) {

//Now split the String value

String[] values = value.split("\\.");

//Then do your parseLong or parseInt on the values of the array

//If you need to do calculations using the value, then change it to

//double using parseDouble

Now where do you not understand?
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  public void myFunction(String value) {

//Now split the String value

String[] values = value.split("\\.");

//Then do your parseLong or parseInt on the values of the array

//If you need to do calculations using the value, then change it to

//double using parseDouble

Now where do you not understand?
got it correctly when i did parseLong instead of parseInt.
Thankyou very much
got it correctly when i did parseLong instead of parseInt.
Thankyou very much
Great! I hope everything is now working fine.
how to handle "ü" character is jsp???
can neone help...urgent help required
how to handle "ü" character is jsp???
can neone help...urgent help required
You really should start your thread for that.
It is considered rude to hijack others' threads.
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
4 posts
views
Thread by cindy liu 
last post: by

3 posts
views
Thread by jeff_zhang446 
last post: by

1 post
views
Thread by Daniel 
last post: by

17 posts
views
Thread by David Scemama 
last post: by

6 posts
views
Thread by patang 
last post: by

5 posts
views
Thread by yekasi 
last post: by
 
9 posts
views
Thread by =?Utf8?B?Qnlyb24=?= 
last post: by

22 posts
views
Thread by xiao 
last post: by
          