Thank you for the very useful document. Following come from it.
" Because users have different decimal separators depending on their locale, never store unit data as formatted strings! Don’t write Length
values out as length.to_s
– instead convert it to a Float
. That way you can be sure that you can read the data back regardless of the locale of the target system because Float.to_s
always use period as decimal separator – and String.to_f
always expect a period. To load a unit from a float stored as string correctly you use: string.to_f.to_l
."
I think “string.to_f.to_l” is really good because in this way we never have errors even wrong number format like “x10”.(“x10”.to_f.to_l = 0). (x10.to_l = error). I think the best way is to read numbers and units from the user, convert them to float, and save numbers and its unit in our format. is it ok?
I used the following ruby code and it works well. If you enter 5in or 5", “5.0 Inch” will save.
if Sketchup::RegionalSettings.decimal_separator == '.'
wallin = wallin.map {|s| s.sub(',', '.')}
else
wallin = wallin.map {|s| s.sub('.', ',')}
end
for i in 0...9
begin
Float(wallin[i]) # if string is not purely a number error will happen.
info[i] = wallin[i].to_l
wallin[i] = wallin[i].to_f.to_s
rescue
if wallin[i].to_f != 0 # if it is 0 means data is not a number.
if wallin[i].end_with?('"', "Inch", "inch", "INCH", "Inches", "INCHES", "inches", "in", "IN")
info[i] = wallin[i].to_f
wallin[i] = "#{wallin[i].to_f} Inch"
elsif wallin[i].end_with?("'", "Foot", "foot", "ft", "FT", "FOOT", "Feet", "feet", "FEET")
info[i] = wallin[i].to_f * 12
wallin[i] = "#{wallin[i].to_f} ft"
elsif wallin[i].end_with?("mm", "MM")
info[i] = wallin[i].to_f / 25.4
wallin[i] = "#{wallin[i].to_f} mm"
elsif wallin[i].end_with?("cm", "CM")
info[i] = wallin[i].to_f / 2.54
wallin[i] = "#{wallin[i].to_f} cm"
elsif wallin[i].end_with?("m", "M")
info[i] = wallin[i].to_f / 0.0254
wallin[i] = "#{wallin[i].to_f} m"
elsif wallin[i].end_with?("Yard", "yard", "YARD", "yd", "YD")
info[i] = wallin[i].to_f * 36.0
wallin[i] = "#{wallin[i].to_f} yd"
else
info[i] = 1000000 # This is out of range number and cause all data will reset.
end
else
info[i] = 1000000 # This is out of range number and cause all data will reset.
end
end
end