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));
}
}
15 7626
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 - 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) ((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
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: cindy liu |
last post by:
Hi,
In .Net, how to convert a string to a double?
Thanks in advance!
Cindy
|
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();
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: xiao |
last post by:
Can I fullfill this task? Using fred and fwrite?
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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
|
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...
| |