473,394 Members | 1,722 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,394 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 2525
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
Oralloy
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,...
0
jinu1996
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...
0
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...
0
tracyyun
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...

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.