Skip to content

Instantly share code, notes, and snippets.

@swoleengineer
Last active April 28, 2017 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swoleengineer/4142143c97e349bdc22094c37044e37b to your computer and use it in GitHub Desktop.
Save swoleengineer/4142143c97e349bdc22094c37044e37b to your computer and use it in GitHub Desktop.
Angular filter for metric from imperial ({ft + in -> cm}, {lbs -> kg})
app.filter('metric', function(authToken){
return function(input, type, inch) {
var isMetric = authToken.getMetric();
if(isMetric) {
if(type == 'cm') {
if(isNaN(input)) {
return 'cm.';
} else {
if(inch) {
return ((((input * 12) * 1) + (inch * 1)) * 2.54).toFixed(2) + ' cm.';
}
else {
return ((((input * 12) * 1)) * 2.54).toFixed(2) + ' cm.';
}
}
} else if(type == 'kg') {
if(isNaN(input)){
return 'kg.';
}else{
var amount = 0.453592 * 1;
return (((input * amount) / 1 ) * 1).toFixed(2) + ' kg.';
}
}
} else {
if(type == 'kg') {
if(isNaN(input)){
return 'lbs.';
}else{
return input + ' lbs.';
}
} else if(type == 'cm') {
if(isNaN(input)){
return "ft., in.";
}else{
return input + "'" + inch + '"';
}
}
}
};
});
@swoleengineer
Copy link
Author

authToken.getMetric() detects if the user wants things in metric or not. This is something saved in the user's schema

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment