为什么我的命令将正面转换为负面不起作用 [英] Why is my command to convert positive to negative not working
问题描述
所以,例如,如果我输入20并执行命令而不是将其设为-20,那么它为20-,为什么?
我是什么尝试过:
So if for example i enter 20 and execute the command instead of making it -20 it makes it 20-, why??
What I have tried:
textBox1.Text = (Double.Parse(textBox1.Text) * -1).ToString();
推荐答案
我刚试过你的代码,它运行正常。程序中必须有其他内容影响您的结果。或者可能是您系统上的区域设置使用尾部减号表示负数。
I just tried your code and it works fine. There must be something else in your program that is affecting your results. Or it may be that the regional settings on your system use a trailing minus sign for negative numbers.
它看起来像
System.Globalization.NumberStyles
设置为AllowTrailingSign
。您可以更改double.Parse
方法,将符号放在这样的数字前面。
It looks like
System.Globalization.NumberStyles
is set toAllowTrailingSign
. You can change yourdouble.Parse
method to place the sign in front of the number like this.
string res=String.Format("{0:#.##;-#.##}", (Double.Parse("200.549") * -1));
//'-200.55'
前3个'#'符号格式化正数。没有'+'符号四舍五入到小数点后两位。第二组3'#'符号格式为负数。一个' - '符号后跟数字四舍五入到小数点后两位。
The first 3 '#'symbols format a positive number. No '+' sign rounded to 2 decimal places. The second set of 3 '#'symbols format a negative number. A '-' sign followed by the number rounded to 2 decimal places.
这篇关于为什么我的命令将正面转换为负面不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!