How do I convert a negative float (like -4.00) to a positive one (like 4.00)?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The best way to flip the number is simply to multiply it by
-1:If you’re not sure whether the number is positive or negative, and don’t want to do any conditional checks, you could instead retrieve the absolute value with
Math.abs():Note, this will turn
-50.00into50(the decimal places are dropped). If you wish to preserve the precision, you can immediately calltoFixedon the result:Keep in mind that
toFixedreturns a String, and not a Number. In order to convert it back to a number safely, you can useparseFloat, which will strip-off the precision in some cases, turning'50.00'into50.