467,173 Members | 1,366 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,173 developers. It's quick & easy.

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
  • viewed: 2244
Share:
2 Replies
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by Khuong Dinh Pham | last post: by
2 posts views Thread by Asbjørn Ulsberg | last post: by
3 posts views Thread by Pete Davis | last post: by
1 post views Thread by Big D | last post: by
2 posts views Thread by Alex Buell | last post: by
4 posts views Thread by alacrite@gmail.com | last post: by
10 posts views Thread by Hank Stalica | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.