简体   繁体   English

将 float 转换为 ushort

[英]Convert float to ushort

I am new to C sharp, and i am trying to do the following but getting an error as not able to convert short to ushort我是 C sharp 的新手,我正在尝试执行以下操作但出现错误,因为无法将 short 转换为 ushort

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

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

Try尝试

ushort offsetIDWrite = Convert.ToUInt16(x);

The lost of significant data is considerable with the two lines displayed.对于显示的两条线,重要数据的丢失是相当大的。

the textbox should be validated as part of the conversion.文本框应作为转换的一部分进行验证。

  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);
     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM