Comparing strings with extra zero
37 次查看(过去 30 天)
显示 更早的评论
Hello, I am wanting to perform a safety check that 2 strings are the same:
send: 297.043 rec: 297.043
send: 298.030 rec: 298.03
send: 300.833 rec: 300.833
Theres a problem when I try to test for string equivalence i.e. the 298.030 value, not sure how to resolve this.
thanks
0 个评论
回答(1 个)
Saurabh
2024-11-13,14:59
When comparing strings for equivalence, even a small difference in formatting (such as trailing zeros) can cause them to be considered different. In this case, the issue arises with the second pair of strings, where one has a trailing zero and the other does not ("298.030" vs. "298.03").
To perform a safety check that considers such cases, consider converting the strings to numerical values before comparing them. This approach ignores formatting differences like trailing zeros.
Use 'str2double' to convert the string representations of numbers to actual numerical values.
Leverage the following link to know more about function:
I hope this helps.
3 个评论
Saurabh
2024-11-13,15:16
Thanks for the reply,
If 0.00001 is too strict for the application, consider increasing the tolerance to accommodate the precision differences. For example, consider using 0.0001 instead?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!