Home / Forum / Discussion and Help for Gmod-Glua-Lua / How do transform a negative number into a positive number? ✔️ Solved

How do transform a negative number into a positive number?

Thread
profile avatar Jenny Fer
⚗️ Support
📖 Posts: 31
↩️ Replies: 42
❤️ Reactions: 18
Jenny Fer Posted 1668719748 (Edited) (129 views)
How do transform a negative number into a positive number?

x = - 7

How to transform a negative number into a positive number or invert the number in Lua GLua for GMod?

And then how to have a number always negative I know I'm asking two things.
⛔️ Sorry, you are not connected, join to leave a reaction

Replies
profile avatar Norda Scripts
🎨 Creator
📖 Posts: 162
↩️ Replies: 76
❤️ Reactions: 25
Norda Scripts Posted 1668721727
Try math.abs(-7)
You will always get a positive value, so in your example math.abs(-7)=7.

And to convert a positive value back to negative you multiply by -1. So math.abs(7)*-1 will give -7. Same result with math.abs(-7)*-1 = -7 because math.abs will always give a positive value.
profile avatar Jenny Fer
⚗️ Support
📖 Posts: 31
↩️ Replies: 42
❤️ Reactions: 18
Jenny Fer Posted 1668724217
Well done, thank you! I had found a solution was to remove the "-" in string and replace it with a blank "" which leaves the number only.
profile avatar Norda Scripts
🎨 Creator
📖 Posts: 162
↩️ Replies: 76
❤️ Reactions: 25
Norda Scripts Posted 1668724549
What you have done should work but it is very greedy, math.abs(x) does this kind of job much better. I mark this post as solved. You asked me the question in English and also in French and I am answering you on both forums. Have a nice day. :)


Sign in or register to reply