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.3 5".
now i want to pass it as a double value to the method format() which is shown below.
when i do Double.parseDou ble i get dTot as 5.037717235E7
CurrencyConvert er.format(Doubl e.parseDouble(F reemarkerTools. formatDecimal(d Tot)));
what else can i do to pass it as 50377172.35 to the method format()
public class CurrencyConvert er {
public static String format(double n) {
if(n<0)
return "("+formatAcc(M ath.abs(n))+")" ;
return formatAcc(n);
}
public static String formatAcc(doubl e n){
//long decPart=0;
String s1 = ""+n;
String[] nums = s1.split("\\.") ;
long decPart = Integer.parseIn t(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(de cPart,"00");
}
private static String format(long n, String format) {
String temp = n + "";
temp = format.substrin g(temp.length() ) + temp;
return temp;
}
public static void main(String[] args) {
System.out.prin tln(CurrencyCon verter.format(5 54406000.40));
}
}
15 7658
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.3 5".
now i want to pass it as a double value to the method format() which is shown below.
when i do Double.parseDou ble i get dTot as 5.037717235E7
CurrencyConvert er.format(Doubl e.parseDouble(F reemarkerTools. formatDecimal(d Tot)));
what else can i do to pass it as 50377172.35 to the method format()
public class CurrencyConvert er {
public static String format(double n) {
if(n<0)
return "("+formatAcc(M ath.abs(n))+")" ;
return formatAcc(n);
}
public static String formatAcc(doubl e n){
//long decPart=0;
String s1 = ""+n;
String[] nums = s1.split("\\.") ;
long decPart = Integer.parseIn t(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(de cPart,"00");
}
private static String format(long n, String format) {
String temp = n + "";
temp = format.substrin g(temp.length() ) + temp;
return temp;
}
public static void main(String[] args) {
System.out.prin tln(CurrencyCon verter.format(5 54406000.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.3 5"
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.parseIn t() of the decimal part.
Trying to parse it, i get NumberFormatExc eption...
Please help me recover frm this
Still i have the same problem.
When i get a string in format() as "50377172.3 5"
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.parseIn t() of the decimal part.
Trying to parse it, i get NumberFormatExc eption...
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(doubl e n){
//long decPart=0;
String s1 = ""+n;
String[] nums = s1.split("\\.") ;
long decPart = Integer.parseIn t(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(de cPart,"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(doubl e n){
//long decPart=0;
String s1 = ""+n;
String[] nums = s1.split("\\.") ;
long decPart = Integer.parseIn t(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(de cPart,"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.4 0" 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.4 0" 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?
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 web form
to upload a TIFF image (scanned images) and convert it to a JPEG
before I insert it into SQL Server. The file upload part works great,
but I can;t convert the image. I certainly don't want to upload it the
the server filesystem, convert...
|
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 problem writing VB Double values to
the file so as the Pascal program can read them as Pascal Real values.
I've managed to find the algorithm to read the Pascal Real format and
convert it to a VB Double, but I cannot figure out the opposite...
|
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 declaration statments (first lines)
e.g.
Public Function ConvertCurrencyToEnglish(ByVal MyNumber As Double) As String
Private Function ConvertHundreds(ByVal MyNumber As String) As String
etc.
| |
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 to a double type. it
came back with 6 digits precision only.
I also have this number, 1159964886109, in char*, and I need to convert
it to uint64... i did a atol, it converted it to a 2147483647, which
truncated 4 bytes for me...
|
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 function
convert(double num);
how can i convert the string value 50377172.35 to double?
|
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 I've seen. The code:
DoubleVariable = Double.Parse(HexString, NumberStyles.AllowHexSpecifier);
results in the error:
"The number style AllowHexSpecifier is not supported on floating point data
|
by: xiao |
last post by:
Can I fullfill this task? Using fred and fwrite?
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |