简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM