简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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