问题
How do I convert a positive Integer to a negative with Delphi? I know you could use ABS(int)
to convert a negative to a positive, but I need it to convert positive to negative.
回答1:
from what I know there is no function for that. you can make
if yourvariable > 0 then
yourvariable := -yourvariable;
回答2:
If you want to be absolutely sure to get a negative result (or zero), use
number := -abs(number)
回答3:
Ehhh... Too late but number := number * -1;
would work too, but this change the symbol of any number, negative to positive and backwards... To ensure a negative value go with @Mef Answer
EDIT: even later, but number := 0 - number;
would work too... This just reverses the symbol as well
回答4:
Let n
be your number and the formula for n-(n*2) = -n
来源:https://stackoverflow.com/questions/9968707/how-do-i-convert-a-positive-number-to-negative-in-delphi