The magnitude()
method of a float is used to convert a float value to its absolute form.
float_value.magnitude()
This methodis only invoked on the float value.
This method returns the absolute value of the float_value
.
# create some float valuesf1 = 2.222f2 = 3.4/-0.222f3 = -(3 + 3) * 2f4 = 20.92# print magnitudesputs f1.magnitudeputs f2.magnitudeputs f3.magnitudeputs f4.magnitude
Lines 2–5: We create four variables f1
, f2
, f3
, and f4
that contain different float values.
Lines 8–11: We invoke the magnitude()
method on these float values and print the returned results to the console, using the puts
method.