How do I convert a positive number to negative in Delphi?

你。 提交于 2020-01-01 08:45:29

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!