export default function TwoDecimalDirective() {
  return {
    restrict: 'A',
    require: 'ngModel',
    link(scope, elem, attrs, ngModel) {
      if (attrs.wsTwoDecimal === 'true') {
        ngModel.$formatters.push(value =>
          /*
           * we want to trim extra decimals not round.
           * 12.4567 --> 1245.67 --> 1245 --> 12.45
           */
          (Math.floor(parseFloat(value) * 100) / 100).toFixed(2));

        ngModel.$parsers.push(value => Math.floor(parseFloat(value) * 100) / 100);
      }
    },
  };
}
