What's the best way to convert from a double to a string?
for example, at the moment I have to do this:
string s = new Double(a + b / c).ToString();
Is there a better way for me to do this so I don't have to create a double
object all the time?
thanks,
/m 8 4080
something like this should do the trick.
string s = Convert.ToStrin g( dDoubleValue );
"Ahjay Muscha" <mu****@no.spam .net> wrote in message
news:OF******** ******@TK2MSFTN GP11.phx.gbl... What's the best way to convert from a double to a string?
for example, at the moment I have to do this:
string s = new Double(a + b / c).ToString();
Is there a better way for me to do this so I don't have to create a double object all the time?
thanks,
/m
Ah thanks, my brain space still mixed in Java and c# a bit :)
Also why is Decimal a class whlie Double is a structure?
/m
"Carlson Quick" <ca******@exten dedsystems.com> wrote in message
news:uM******** ******@TK2MSFTN GP12.phx.gbl... something like this should do the trick. string s = Convert.ToStrin g( dDoubleValue );
"Ahjay Muscha" <mu****@no.spam .net> wrote in message news:OF******** ******@TK2MSFTN GP11.phx.gbl... What's the best way to convert from a double to a string?
for example, at the moment I have to do this:
string s = new Double(a + b / c).ToString();
Is there a better way for me to do this so I don't have to create a
double object all the time?
thanks,
/m
Muscha wrote: Ah thanks, my brain space still mixed in Java and c# a bit :)
Also why is Decimal a class whlie Double is a structure?
Decimal is a structure, too.
--
mikeb
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
They are all structures. Unlike in Java, where you have double and
java.lang.Doubl e, in C# a System.Double *is* a double. Oh by the way,
regarding your initial question:
~ double a = 5.0;
~ double b = 4.3;
~ double c = 0.7;
~ string s = (a + b + c).ToString();
~ Console.WriteLi ne(s);
Muscha wrote:
| Ah thanks, my brain space still mixed in Java and c# a bit :)
|
| Also why is Decimal a class whlie Double is a structure?
|
| /m
- --
Ray Hsieh (Ray Djajadinata) [SCJP, SCWCD]
ray underscore usenet at yahoo dot com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org
iD8DBQE/oGCKwEwccQ4rWPg RAkbEAJ9OCLOkIm 2iB7ajMiKTkxqal g1OrwCfSGSb
eJ0sy+TOue0iqeb XJ89uef8=
=OrnI
-----END PGP SIGNATURE-----
Concatenating with an empty string is another way to do it:
"" + (a + b / c)
Probably a bit less efficient than (a + b / c).ToString() because it
translates into String.Concat(" ", (a + b / c)).
Bruno.
"Ahjay Muscha" <mu****@no.spam .net> a écrit dans le message de
news:OF******** ******@TK2MSFTN GP11.phx.gbl... What's the best way to convert from a double to a string?
for example, at the moment I have to do this:
string s = new Double(a + b / c).ToString();
Is there a better way for me to do this so I don't have to create a double object all the time?
thanks,
/m
Bruno Jouhier [MVP] <bj******@clu b-internet.fr> wrote: Concatenating with an empty string is another way to do it: "" + (a + b / c) Probably a bit less efficient than (a + b / c).ToString() because it translates into String.Concat(" ", (a + b / c)).
It's also ugly (IMO) because it doesn't reflect the desired goal
explicitly - there's nothing about converting a double to a string
which naturally has anything to do with concatenation or the empty
string.
I would use (a+b+c).ToStrin g() or Convert.ToStrin g(a+b+c). Both of say
explicitly what they're trying to do.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
That's a matter of taste, I think.
This string conversion syntax has one advantage, though: it is safer when
converting reference types to string because it handles the null case
smoothly. "" + obj will always work (in C#, it gives "", in Java, it gives
"null"), while obj.toString() will throw an exception if obj is null.
With doubles, you are on the safe side anyway, because they are value types
and thus never null. And I'm probably using this syntax rather than
d.ToString() because I've been biased by Java which does support instance
methods on primitive types.
Bruno.
For example: ("" + obj
"Jon Skeet [C# MVP]" <sk***@pobox.co m> a écrit dans le message de
news:MP******** *************** *@msnews.micros oft.com... Bruno Jouhier [MVP] <bj******@clu b-internet.fr> wrote: Concatenating with an empty string is another way to do it: "" + (a + b / c) Probably a bit less efficient than (a + b / c).ToString() because it translates into String.Concat(" ", (a + b / c)).
It's also ugly (IMO) because it doesn't reflect the desired goal explicitly - there's nothing about converting a double to a string which naturally has anything to do with concatenation or the empty string.
I would use (a+b+c).ToStrin g() or Convert.ToStrin g(a+b+c). Both of say explicitly what they're trying to do.
-- Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
Bruno Jouhier [MVP] <bj******@clu b-internet.fr> wrote: That's a matter of taste, I think.
To some extent, but I think it's fairly clear that the "convert to a
string" notion is at least more explicit in
Convert.ToStrin g(...)
or
(...).ToString( )
than in
""+x
This string conversion syntax has one advantage, though: it is safer when converting reference types to string because it handles the null case smoothly. "" + obj will always work (in C#, it gives "", in Java, it gives "null"), while obj.toString() will throw an exception if obj is null.
In Java I'd always use String.valueOf( ) though - which again is
explicit, and gives the same answer.
See http://www.pobox.com/~skeet/java/stringconv.html for more of my
reasoning about this.
With doubles, you are on the safe side anyway, because they are value types and thus never null. And I'm probably using this syntax rather than d.ToString() because I've been biased by Java which does support instance methods on primitive types.
.... but has a better way of doing it anyway :)
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Danny Woolston |
last post by:
Hi
I have a double that i want to round to a certain number of decimal places
this is the code i use
Return Math.Round(mdNumber, mbDecimalPlaces)
mbDecimalPlaces is the number of decimal places that i want, but it doesn't
use them with two decimal places i still get 0.0 or 9.9
|
by: Newbie |
last post by:
Why can I not get a decimal value from my double using:
double test;
test = 1/3;
MessageBox.Show(test.ToString());
|
by: Rod Brick |
last post by:
I'm trying to print a Double in straight decimal form, not exponential. I can't seem to accomplish this. This seems like it should
be simple enough. The output I'm looking for is "0.00001", not "1E-05".
I'm obviously baffled by the NumberFormatInfo class, I can't seem to make it do what I want. And, I've tried the following, all of
which have failed to modify the original exponential form. The commented out "D7" statement throws a...
|
by: Markus Kling |
last post by:
"double.Parse(double.MaxValue.ToString())" yields the following Exception:
Value was either too large or too small for a Double.
at System.Number.ParseDouble(String value, NumberStyles options,
NumberFormat
Info numfmt)
at System.Double.Parse(String s, NumberStyles style, NumberFormatInfo info)
at System.Double.Parse(String s)
...
|
by: Alpha |
last post by:
I have a window application. In one of the form, a datagrid has a dataview
as its datasource. Initial filtering result would give the datavew 3 items.
When I double click on the datagrid to edit the selected lie item at which
case I would pop up a separate dialog box to do so, in the debugging code,
the dataview.count would return 0. I get a error message because I tried to
get values out of a dataview that holds 0 items. Does anyone...
| |
by: D. Shane Fowlkes |
last post by:
Here's a good one. I've been using an Excel spreadsheet for the past couple
of years to calculate a file's Estimated Download Time based off of a solid
50kbs connection (dial up). This is for a downloads page such as:
http://www.drpt.virginia.gov/downloads/selectedcat.aspx?ID=12
The formula is basically this:
=(((C4*1000*8)/50000)/84600)*1.1
(Estimate is calculated on a modem dial up connection of 50Kbs and transfer
|
by: JWest46088 |
last post by:
I keep getting these error messages:
area(double,double) in Rectangle cannot be applied to ()
return "Area: " + Rectangle.area() + "\tCircumference: " + Rectangle.perimeter(); ^
perimeter(double,double) in Rectangle cannot be applied to ()
return "Area: " + Rectangle.area() + "\tCircumference: " + Rectangle.perimeter();
^
setSides(double,double) in Rectangle cannot be applied to (double)...
|
by: ThunderMusic |
last post by:
Hi,
We have a part of our application that deals with millions of records and do
some processing of them. We've achieved a pretty good performance gain by
developping a custom DateTime.ToString and a custom int.ToString, but we
can't find any clue on doing for decimal and double, which would be about
half the load if they are put together (decimal and double). By changing our
DateTime and Int ToStrings, we achieved a 84% performance gain,...
|
by: Dan |
last post by:
What is the equivalent of Double.toString from Java in C#
In Java
new Double(4).toString() = "4.0"
but in C#
new Double(4).ToString() = "4"
|
by: Lior Bobrov |
last post by:
Hi ...
How to convert a variable of type Double to String , *preserving* the
original value of the Double variable , as is , in a short (convenient) way
?
For example , if there are two variables :
Dim dblResult as Double
Dim strResult as String
|
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: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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: 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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |