문제

I have done this for my division code, and instead of the numbers turning out like 10.55555 or 2.55555, how can it be shortened to 10.555 and 2.555

decimal d = numericUpDown1.Value / numericUpDown2.Value;
label1.Text = "Ratio: " + d.ToString();
도움이 되었습니까?

해결책

Try

decimal d = numericUpDown1.Value / numericUpDown2.Value ;
label1.Text = string.Format( "Ratio: {0}" ,
                Math.Round(d,3) ,
                MidpointRounding.ToEven
                ) ;

Or

decimal d = numericUpDown1.Value / numericUpDown2.Value ;
label1.Text = string.Format( "Ratio: {0:0.000}" ,
                Math.Round(d,3) ,
                MidpointRounding.ToEven
                ) ;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top