1

I have two variables in my react component. I want to divide one variable from another and print the value for only one decimal place. So, I choose the substring method as following:

 <div className="col-md-8">
      {(total_star / total_user).substring(0,2)}
 </div>

In the output I am getting the following error:

TypeError: (total_star / total_user).substring is not a function

Kindly help to print substring in jsx.

0

1 Answer 1

3

That is because the result is a float not a string, you could use .toFixed(...) instead of substring

(total_star / total_user).toFixed(2);

Or convert the result to string and the use .substring(...)

(total_star / total_user).toString().substring(0,2)

The first example may not get you the result you're looking for because .substring(0, 2) will give you the first two characters and .toFixed(2) will give you the result with two decimals after the decimal point.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.