precision of digits durring conversion
-
float Width = 47;
textBox1.Text = System.Convert.ToString(Width);Consider above statements. after executing this command the content of textBox1.Text will be 46.99 instead of 47; I've solved this problem by some functions, but I want to see is there any tuning in C# 2010 that I missed it? thanks
-
float Width = 47;
textBox1.Text = System.Convert.ToString(Width);Consider above statements. after executing this command the content of textBox1.Text will be 46.99 instead of 47; I've solved this problem by some functions, but I want to see is there any tuning in C# 2010 that I missed it? thanks
I'm not sure why you are seeing 46.99. When I run this code, I see 47 written out both times:
float myVal = 47;
string text = Convert.ToString(myVal);
Console.WriteLine(myVal.ToString());
Console.WriteLine(text);*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
-
float Width = 47;
textBox1.Text = System.Convert.ToString(Width);Consider above statements. after executing this command the content of textBox1.Text will be 46.99 instead of 47; I've solved this problem by some functions, but I want to see is there any tuning in C# 2010 that I missed it? thanks
There is no tuning for this. This is a side effect of trying to store an infinite number of floating point numbers in a finite amount of bits. Read this[^] for more information.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
float Width = 47;
textBox1.Text = System.Convert.ToString(Width);Consider above statements. after executing this command the content of textBox1.Text will be 46.99 instead of 47; I've solved this problem by some functions, but I want to see is there any tuning in C# 2010 that I missed it? thanks
- Avoid
float
; tryDecimal
ordouble
1) AvoidConvert
; in this case useToString
, perhaps with a format parameter 2) AvoidTextBox
for numeric values; use aNumericUpDown
- Avoid