简体   繁体   中英

inverting positive CGFloat to negative value

This might be a very dumb question, but if I have a CGFloat with value of 44.0, how do I make it to -44.0? I tried doing -myFloat and 0 - myFloat but it gives me 0. Why is this? Also I don't want a multiply by -1 answer

Its very simple dude. Just multiply it with -1.

i.e. 44.0 * -1 = -44.0

Update:

Solution 2 as mentioned by Inafziger

yourNo = 0 - yourNo

If myFloat is equal to 44.0, then the expression -myFloat will be evaluated as -44.0. You've got a bug somewhere else in your code. Can you post the expression where it "gives you 0"?

Using a clumsy workaround will just make your code harder to read and even worse to debug in the long run. Let's sort out the actual problem. :)

Okay, so you can't multiply by -1, how about:

myFloat = 0 - myFloat;

or

myFloat = myFloat - 2 * myFloat;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM