Search code examples
godifference

Calculate difference between two numbers and get the absolute value


I want to find the difference between two numbers in Go and the result should not be in "-".

Please find my code below:

    dollarValue := 240000 - 480000  

The result is "-240000". But my expected output is just "240000". Can anybody help on how to calculate the difference between these two numbers.


Solution

  • Your title is misleading. It should be states without negative instead of - operator.

    Basically what you want to get is the absolute different between two numbers

    You have two options:

    • Use if/else condition to return the positive result if the result is negative
    • Use math.Abs (need to convert from/to float)