I need to convert string to float, but there can be different input string formats, such as '1234,5'
or '1234.5'
or '1 234,5'
or '1,234.5'
or whatever. And I can not change locale decimal pointer or thousands separator, because I may not know what data I will get in advance.
Is there a way or method or library to parse and convert to float this kind of locale-specific values without knowing which locale is used?
PS Is there any solution exists for the same problem with dates?
TIA.
You can make some assumptions on which character is the thousands separator and which is the decimal point. However, there is a case where you cannot know for sure what do do:
.
or ,
. If it occurs more than once, the number does not have a decimal point and that character is the thousands separator 123.456
or 123,456
might be the number 123456
or 123.456
. However, with a number like 123.45
- ie the number of digits after the potential thousands separator not being a multiple of three - you can assume that it's a 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.