473,320 Members | 1,993 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to convert string to double ..please help

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));
}
}
Mar 13 '07 #1
15 7626
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[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

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
Like I already pointed out before, change your method

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?

Please c the code given
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?

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

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
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...

Please help me recover frm this
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...

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.
Mar 13 '07 #6
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) ((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[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");
}
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
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
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]);
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[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

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
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
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
You really should start your thread for that.
It is considered rude to hijack others' threads.
Mar 13 '07 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: cindy liu | last post by:
Hi, In .Net, how to convert a string to a double? Thanks in advance! Cindy
3
by: jeff_zhang446 | last post by:
Hi, I try to convert double to string as below: std::string cnvrtToString(double lValue) { std::ostringstream lStream; lStream << lValue; return lStream.str();
1
by: Daniel | last post by:
I have looked everywhere on the web for an answer to this and the only thing I can find is converting the image format when the file is present on the local filesystem. What I want to do is use a...
17
by: David Scemama | last post by:
Hi, I'm writing a program using VB.NET that needs to communicate with a DOS Pascal program than cannot be modified. The communication channel is through some file databases, and I have a huge...
6
by: patang | last post by:
Could someone please tell me where am I supposed to put this code. Actually my project has two forms. I created a new module and have put the following code sent by someone. All the function...
5
by: yekasi | last post by:
Hi, I have this number, 1.5283632704970307 in char*, and I need to convert it to a double type in C++ that is capable of storing that number of precisions.... I tried doing atof and storing it...
8
by: shiniskumar | last post by:
Ive got a double variable dTotal =5.037717235E7 i formatted it using deciFormat.format(dTotal) and got the String value of dTotal=50377172.35 now i have to pass this value as an argument to a...
9
by: =?Utf-8?B?Qnlyb24=?= | last post by:
How do you convert a hex string into a double using C# in framework v2.0? I've seen this question answered before, but apparently something has changed in 2.0 that negates the old solution that...
22
by: xiao | last post by:
Can I fullfill this task? Using fred and fwrite?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.