I’m writing a function to take shorthand values and convert them into a standardized numeric format. Is there any standard code out there that would do “best possible” conversion of arbitrary measurement text and turn it into numeric measurements if the text is valid?
I guess I’m looking for something like bool TryParseMeasurement(string s, out decimal d). Does anyone know of a function like this?
Here’s an example of some of the input values I’ve seen:
Imperial
- 6 inches
- 6in
- 6”
- 4 feet 2 inches
- 4’2”
- 4 ‘ 2 “
- 3 feet
- 3’
- 3 ‘
- 3ft
- 3ft10in
- 3ft 13in (should convert to 4’1”)
Metricc
- 1m
- 1.2m
- 1.321m
- 1 meter
- 481mm
Here’s some code we wrote in an app quite some time ago, where we were doing something similar. It’s not the best, but you may be able to adapt, or get some sort of jumping off point.