How to convert string to number and process underscores? (e.g. '57_77_' to 57.77)
조회 수: 10(최근 30일)
How do you convert a string with underscores to a number, namely '57_77_' to 57.77? What commands would you use?
I am looking through the documentation, e.g. join, compose, sprintf, extractBefore, trying to figure out how to process such a string, namely to execute the steps:
- delete final '_'
- convert '_' to decimal point '.'
- convert string to number
Stephen23 2018년 3월 14일
편집: Stephen23 2018년 3월 14일
Faster and more efficient than using str2num (which hides a slow eval call inside) is to simply use the low-level function sscanf:
ans = 57.770
This is ten times faster than the accepted answer (1e4 iterations):
Elapsed time is 0.277028 seconds. % my code
Elapsed time is 2.63426 seconds. % accepted answer
Birdman 2018년 3월 14일
편집: Birdman 2018년 3월 14일
The wise thing would be to first convert underlines to dots by using regexprep or strrep:
and then delete the last character by
Other way would be doing this by using regexp: