imsaiful
imsaiful

Reputation: 1614

How to print substring in React JSX?

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.

Upvotes: 1

Views: 5297

Answers (1)

Titus
Titus

Reputation: 22474

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.

Upvotes: 3

Related Questions