将float转换为ushort

时间:2016-10-06 19:57:49

标签: c#

我是C sharp的新手,我正在尝试执行以下操作,但因为无法将短转换为ushort而出现错误

double x = Convert.ToDouble(textBox6.Text) * 10;
ushort offsetIDWrite = Convert.ToInt16(x);

3 个答案:

答案 0 :(得分:1)

使用ushort offsetIDWrite = Convert.ToUInt16(x);

答案 1 :(得分:0)

显示两行显着丢失重要数据。

文本框应作为转换的一部分进行验证。

  try {
        ushort number = UInt16.Parse(textbox6.text);
        Console.WriteLine("'{0}' --> {1}", textbox6.text, number);
     }
     catch (FormatException) {
        Console.WriteLine("'{0}' --> Bad Format", textbox6.text);
     }
     catch (OverflowException) {   `enter code here`
        Console.WriteLine("'{0}' --> OverflowException", textbox6.text);
     }
     catch (ArgumentNullException) {
        Console.WriteLine("'{0}' --> Null", textbox6.text);
     }

答案 2 :(得分:-1)

尝试

ushort offsetIDWrite = Convert.ToUInt16(x);