How to use substring in React Native?

1

How to use substring in React Native?

if (total >= 1000){
      const total2 = total.substr(total.lenght - 3, total.lenght);
      total = total2 + '.' + total.substr(total.lenght - (total.lenght - 1), total.lenght - 3);
    }

Error:

  

undefined is not a function (evaluating 'total.substr (total.lenght - 3, total.lenght);')

My intention is to check if the value is greater than or equal to 1000, divide the string into two parts and put the point in the third house backwards: 1,000

    
asked by anonymous 05.01.2018 / 05:16

1 answer

3

You have a syntax error in your code: the correct one is length and not lenght (the h at the end).

In addition to being wrong with substr . The error in question is because the total variable is not a string . substr will only work with strings.

Previously convert the variable total to string with .toString() and then correctly apply substr to get the desired result:

total = 2000;
if (total >= 1000){
   const total_string = total.toString();
   const total2 = total_string.substr(0, total_string.length - 3);
   total = total2 + '.' + total_string.substr(total_string.length - 3, total_string.length);
   alert(total);
}
    
05.01.2018 / 05:49