Ok… first off… I’m an American, and historically Americans know little about day to day use of the Metric system.
I have a need to create a custom textfield that will accept a measurement, that can be in either Metric or Imperial (ft/inches)
The value needs to be checked that it is valid, and then converted to inches (regardless of its input format)
an Imperial value could be entered as 12’3" or 12ft 3 inches
the question is what various formats should I deal with for Metric.
M = meters
mm and cm
would a Metric user ever enter 10M 3cm or would they use 10.3M?
Internally this app will deal in strictly INCHES (and fractions), but convert to/from for input/display purposes
I can’t speak for all Metrics of course, but 10m 3cm seems very weird to me.
Instead of M I would always use m. Also, 10m 3cm is not 10.3 m, but 10.03m. I would expect most people in my country to write it as 10,03 m, (using a comma), but I always use a dot as decimal separator. Fun.
As metric guy, I’d say you should accept the value using the higher precision you allow.
If precision is up to cm. then allow to enter it as a whole number. E.g: 103 cm. (equal 1m and 3cm)
But this is up to you and the context. You can even decide to use meter as the base and then allow the enter 1,03 (pay attention the comma is the decimal separator).
The decimal separator is something you also have to consider indeed. You can decide to allow centimeters only to avoid handling decimals.
Then, all ends up to what you are sizing. But I suggest to avoid separate input fields for e.g: meter and cm.
Ok… here is what I came up with… it will accept either Metric and/or Imperial[US] values… you can even mix them
you can also include fractions (1/2, 3/4 etc), although I doubt a metric user would want to do such, but you could
if accepts both world spelling for “meter” (metre)
internally it translates metric units to other values for ease of parsing, the user never sees this
Public Function String2Dist(s as string,byref error as boolean) as double
// Method : String2Dist [String to Distance]
// Author : R.David S [22Mar2018]
// Input : a string representing a Metric or Imperial distance measurement
// Output : a double representing input value as INCHES, error will be true if conversion not possible
// Assumption : app has a boolean value called optionMetric to decide default units
//
Dim unit As String
Dim x As Integer
Dim y As Integer
Dim t As String
Dim v() As String
Dim value As Double
Dim answer As Double
Dim i As Integer
If InStr(s,"|")>0 Or InStr(s,"X")>0 Or InStr(s,"U")>0 Then
error=True
Return 0
End If
//
//t=ReplaceAll(t," ","")
s=Replace(s,"s","") // remove plurals
// metric units [use c=cm u=mm x=Meter]
s=Replace(s,"metre","meter") // different spelling
s=Replace(s,"centimeter","c|")
s=Replace(s,"cm","c|")
s=Replace(s,"millimeter","u|")
s=Replace(s,"mm","u|")
s=Replace(s,"meter","X|")
s=Replace(s,"M","X|") // to Separate from mm
// imperial units
s=Replace(s,"feet","'|")
s=Replace(s,"foot","'|")
s=Replace(s,"ft.","|'")
s=Replace(s,"ft","'|")
s=Replace(s,"yard","y|")
s=Replace(s,"yd.","y|")
s=Replace(s,"yd","y|")
s=Replace(s,"inche",ChrB(34)+"|")
s=Replace(s,"inch",ChrB(34)+"|")
s=Replace(s,"in.",ChrB(34)+"|")
s=Replace(s,"in",ChrB(34)+"|")
// c=cm u=mm x=meter '=foot "=inch
v=Split(s,"|")
For i=0 To v.ubound
s=Trim(v(i))
If s="" Then Continue
unit=Right(s,1)
// no unit provided, so assume based on OptionMetric value
If IsNumeric(unit) Then
unit=ChrB(34) // assume inches as missing unit
If optionMetric Then unit="u" // mm
Else
s=Trim(Left(s,Len(s)-1))
End If
//
// does it contain a "fraction" [3/4]?
//
x=InStr(s,"/")
If x>0 Then
value=0
y=InStr(s," ")
If y<x Then
value=CDbl(Left(s,y-1))
s=Trim(Mid(s,y))
x=InStr(s,"/")
End If
t=Left(s,x-1)
s=Mid(s,x+1)
If IsNumeric(s) And IsNumeric(t) Then
value=value+(CDbl(t)/CDbl(s))
s=Format(value,"#####.#####")
End If
End If
//
If Not IsNumeric(s) Then
error=True
Exit For
End If
value=CDbl(s)
Select Case unit
Case "'"
answer=answer+(value*12)
Case ChrB(34)
answer=answer+value
Case "y" // yards = 3 feet
answer=answer+(value*36)
Case "u" // mm
answer=answer+(value*0.0393701)
Case "c" // cm
answer=answer+(value*0.393701)
Case "x" // meter
answer=answer+(value*39.3701)
Case Else
error=True
Exit For
End Select
Next i
Return answer // in "inches"
End Function
Feel free to use it in your own projects, just please give credit someplace
If you have improvements, please let me know so we can all benefit.
FYI… I also have a method for the OPPOSITE of this… value to string
What will happens if je donne une rponse en Franais ?
(no answer required).
There are only some solutions: either you contine to use the Colonial neasures (inch, gallon, etc.) or you use the widely used (the rest of the world) measures.
Compare (apple and oranges) the date format:
a. YYYY/MM/DD, DD/MM/AAAA etc. (how many others ?
b. Jews use a value (> 6700), Muslims > 1400, Christians: 2018)
and I do not talk about the hours (is GMT still used ?)
Did I talk about the Franc ? (French money since and until 2002)
Old people (older than me, but not only) still use the way to talk that was in use before the 1958 (Old Franc), some still use the post 1958 value (/100 from the previous value).
Relative younger people use the (and mosy of Europe use Euro since 1999-01-01).
What a mess.
The only solution (for today as for tomorrow) is to use international (ISO) values, BUT always explain to the user your software deals with them.
After all, you put a stamp in a letter and it goes to destination (everywhere from your state/country to all continents
Same apply for the phone numbers !
Emile… I believe most of us are aware that the world uses various formats for almost everything,… however my posting was focused on DISTANCE, and for that there are just two MAJOR methods (yes, there are others, but they are not worth the effort to support in modern software…)
As a French man whose country invented the metric system, I can attest that we tend to always use decimal, and never fractions.
The most common small size value is centimeters, although very small sizes would in millimeters. Up to 50 centimeters, usually.
After that, meters are used.
In Europe, decimal separator is comma.
However, I noticed that Amazon uses the dot in Canada, and Mexico, both countries using Metric.
In Europe, thousands separators are less consistent. In France, thousands separator is the space character. In Switzerland, it is the apostrophe. In some other countries, dot is used.
as much I assumed… but since it was easier to just allow fractions regardless of Metric vs Imperial I did so.
The method I posted actually allows you to mix and match both systems, as it breaks the input string into individual components and evaluates each on its own merit.
so something like
dim error as boolean
call String2Dist("5 feet 3.7 inches 6 meters 3202 cm 5 yards 3 mm",error)
should give the correct answer
and it SHOULD deal with comma vs dot correctly based on locale
While I doubt America will ever embrace the Metric system in day to day life… I do find it interesting how inconsistent the Imperial system is. Metric obviously is based on multiples of 10…
where the Imperial System is all over the place
fractions of inches are in multiples of 1/32 (for most measurements) 1/32, 1/16, 1/8, 1/4, 1/2 etc.
Feet are multiples of 12
Yards of multiples of 3 feet
Miles are 5280 feet
But when ever I run across Metric… I always need to convert in my head… remembering the magic number for the most part is 2.54, just remember where the decimal point goes
After some like ten years in the US, I had become fairly fluent at converting mentally. The only conversion I could not perform was Fahrenheit and Celsius
Always good to read these kind of posts.
In Australia we use metric with a decimal point as seperator, not a comma.
Height is in cm e.g. 175 cm
Weight is in Kg e.g. 101.5
Distance is in Km, metres, cm
If measuring for carpentry, use mm
E.g, width of 20 mm length 1200 mm (1.2 m)
Note the use of capitals Vs lower case.
I have written an app to calculate heparin doses for patients undergoing radio frequency ablation for atrial fibrillation. It used height, weight, ■■■ to calculate an estimated blood volume.
I would be very interested to see Dave Ss reverse calculation from pounds and inches to Kg and cm
Right now the reverse of what I posted, it DISTANCE based only, but with proper incentive I could easily make an entire class that would do distance, weight and volume
Just reflecting on your original post Dave: I have a need to create a custom textfield that will accept a measurement, that can be in either Metric or Imperial (ft/inches)
Why not have two text fields, one for imperial and one for metric. You could then enforce the date entered to be if the format you desire, as indicated in your labels, then if the metric data field is entered, do the conversion and update the imperial display. If entered in the imperial field, update the metric one.
Either way, you can use the data from the imperial text field to do your calculations
As to an incentive, I can do your tadpole manual in .pdf HTML, word format. I havent forgotten, just been flat out like a lizard drinking on the app I am writing ( desktop and iOS versions)
My app isn’t a conversion program… It is a home design program, where the user might be in American and user Imperial measuements, or in Europe with Metric… So each textfield that accepts a measurement (height, width etc) can accept it either way, and internally store and do the math on “inches”… later spitting it back out in metric if asked to do so
Ok
How about a setup page where the user accepts a contract to use either metric or imperial as the default data entry? You could also check location and automatically determine if commas or decimal points should be used, although this may it be necessary as your conversion function could easily check for that