[英]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.