[英]How to change the sign to a decimal number in C#
我需要一个控制台应用程序,将符号更改为某个键入的数字。 你输入 10,它给你 -10。 等等。 我已经设法做到了,但如果我输入 1.5,我就做不到。 或任何十进制数。 我得到“输入字符串的格式不正确”。 这就是我所做的。
string inputData = Console.ReadLine();
int a = Convert.ToInt32 (inputData);
int b = a * (-1);
Console.WriteLine(b);
Console.ReadLine();
decimal
作为变量类型Convert.ToDecimal
而不是ToInt32
-a
就足够了string inputData = Console.ReadLine();
decimal a = Convert.ToDecimal (inputData);
decimal b = -a;
Console.WriteLine(b);
Console.ReadLine();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.