Hello,
Try this link where you have many solutions for your problem
how to formate textbox value .....in window application[
^]
Allso I sugest that you simplify code :
Use variable type of double for value of "Convert.ToDouble(s)" because it is too complex inside Math.Round()
Sometimes this statment
for(int i=0;i<=s.Length-1;i++)
won't work correct ( I still didn't find out why ) so I use
for(int i=0;i<=s.Length-1;i=i+1)
And you have error in selecting type of int for variable norm, it should be type of double because int type is too small for a range of numbers that may appear during the work of program.
Avoid variable declaration in the same command that you use in the program,
exmpl :
for(int i=0;i<=s.Length-1;i++)
You allso have error in condition
for stopping the for..next loop, it should be i < s.Lenght-1
I have changed your code so now it gives this result for your example 18668951 to 1,867 or 11256,125698 to 1,126 and it should be like that.
It looks like this :
void Button1Click(object sender, EventArgs e)
{
string s = "";
double norm = 0;
double number = 0;
double s1 = 0;
int i = 0;
int lenght = 0;
s = textBox1.Text;
number = Convert.ToDouble(s);
s1 = Math.Round(number,3)*1000;
lenght = s1.ToString().Length;
norm=1;
for(i=0;i<lenght-1;i=i+1)
{
norm = norm * 10;
}
label1.Text =Math.Round((s1/norm),3).ToString() ;
}
You should allso take care of relevance of entered number , if it is not number at all Convert.ToDouble(s) won't work.
Al the best
Perić Željko