472,951 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Converting number to String - Different Approaches

mac
Assume you have a textbox named "displayValue"

What is the difference (if any) between these two approaches:

1.)
float var;
var = 0.42F;
displayValue.Text = var.ToString

2.)
float var;
var = 0.42F;
displayValue.Text = Convert.ToString(var);
Is one way generally preferred over the other?
Thanks,

Mac
Jul 20 '06 #1
2 2495
These both type casts are almost identical.

Convert.ToString just call your value.ToString(CultureInfo.CurrentCulture);
For some types it miss CultureInfo.CurrentCulture

--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche


"mac" wrote:
Assume you have a textbox named "displayValue"

What is the difference (if any) between these two approaches:

1.)
float var;
var = 0.42F;
displayValue.Text = var.ToString

2.)
float var;
var = 0.42F;
displayValue.Text = Convert.ToString(var);
Is one way generally preferred over the other?
Thanks,

Mac
Jul 20 '06 #2
"mac" <no**@none.comwrote:
Assume you have a textbox named "displayValue"

What is the difference (if any) between these two approaches:

1.)
float var;
var = 0.42F;
displayValue.Text = var.ToString
ToString() can accept IFormatProvider and a string format, making it
more flexible. If you have a custom value type and it overrides
ToString(), then calling it directly on the instance won't cause boxing.
2.)
float var;
var = 0.42F;
displayValue.Text = Convert.ToString(var);
This will call var.ToString, and may cause boxing if your value type is
not special-cased by Convert (float is, of course).
Is one way generally preferred over the other?
I personally never use the Convert class except for Base64 conversions.
For most "display value" kind of conversions, I typically use
string.Format().

-- Barry

--
http://barrkel.blogspot.com/
Jul 20 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: TK | last post by:
I'm used to programming in c or c++ in which my problem is simple. I want to be able to enter a value on a page (like 3.2), and then read it as a 32-bit float and break it into it's individual...
16
by: Khuong Dinh Pham | last post by:
I have the contents of an image of type std::string. How can I make a CxImage object with this type. The parameters to CxImage is: CxImage(byte* data, DWORD size) Thx in advance
2
by: Asbjørn Ulsberg | last post by:
Hi. I'm trying to convert Brady Hegberg's great RTF2HTML VB 6.0 module to C#. I've managed to convert the VB code to VB.NET, which gave me the following code: Option Strict On Option...
3
by: Pete Davis | last post by:
I've never done this in C# so I don't know what the appropriate way of doing it is. I've got an array of bytes and I need to convert the array into "usable" data. For example, the first 4 bytes...
1
by: Big D | last post by:
I wrote a program in jscript that takes two arguments: 1. Location 2. Number The purpose of script generates a IP that is different for each location and used to assign Distribution Center...
2
by: Alex Buell | last post by:
Is there an elegant way of converting strings containing digits between different number bases in C++? I.e.: 10 (base 2) = 2 (base 10) FF (base 16) = 256 (base 10) F (base 16) = 1111 (base 2)...
4
by: alacrite | last post by:
I have a class that I want to turn its contents into csv file. I want to be able to set the value of the delimiter, the name of the file it gets saved to, the path of that file, and maybe a few...
3
by: RG | last post by:
I am reading in from a serial port a 16 bit number from 0 to 65536. It is being read as a string from my microcontroller into VB using DEC5 or 5 digits are displayed. I need to display this value...
10
by: Hank Stalica | last post by:
I'm having this weird problem where my code does the following conversion from string to float: 27000000.0 -27000000.00 2973999.99 -29740000.00 2989999.13 -2989999.25 The number on the left...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...

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.