Skip to content

Instantly share code, notes, and snippets.

@faforty
Last active November 26, 2019 12:41
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 faforty/92bfb08e38340f8c449c6baee7249f90 to your computer and use it in GitHub Desktop.
Save faforty/92bfb08e38340f8c449c6baee7249f90 to your computer and use it in GitHub Desktop.
numFormatted(number, is_full_conversion) {
let msrm = parseFloat(number);
if (msrm < 1) {
number = {
value: msrm.toFixed(2),
txt: null
};
} else if (msrm >= 10000) {
if (!is_full_conversion) {
number = {
value: this.dCapacityFormatted(this.numberFormattedByThousands(msrm)),
txt: Em.i18n.t('1000').toString()
};
} else {
if (msrm < 1000000) {
number = {
value: this.dCapacityFormatted(this.numberFormattedByThousands(msrm)),
txt: Em.i18n.t('1000').toString()
};
} else if (msrm >= 1000000 && msrm < 1000000000) {
number = {
value: this.dCapacityFormatted(this.numberFormattedByMillions(msrm)),
txt: Em.i18n.t('1000000').toString()
};
} else if (msrm >= 1000000000) {
number = {
value: this.dCapacityFormatted(this._getNumber(msrm, 1000000000)),
txt: Em.i18n.t('1000000000').toString()
};
}
}
} else {
number = {
value: this.dCapacityFormatted(this._getNumber(msrm, 1, 2)),
txt: null
};
}
return number;
}
getNumber(num, delimiter, fixed) {
return this._getNumber(num, delimiter, fixed);
}
getNumberFormatted() {
return this.numberFormatted(this._getNumber(...arguments));
}
dCapacityFormatted(string) {
if (Em.isEmpty(string)) {
return '';
}
string = string.toString();
let separatorIndex = string.indexOf('.');
let isFloat = separatorIndex > 0 && separatorIndex !== string.length - 1;
let integer = isFloat ? string.slice(0, separatorIndex) : string;
let decimal = isFloat ? string.slice(separatorIndex + 1) : '';
integer = integer.replace(/\B(?=(\d{3})+(?!\d))/g, ' '); // в зависимости от локали надо делить символами, принятыми в стране, а не только пробелами
return decimal ? [integer, decimal].join('.') : integer; // разделитель целой и дробной части в зависимости от страны тоже должен быть разный
}
numberFormatted(string) {
if (Em.isEmpty(string)) {
return '';
}
return string.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ' '); // в зависимости от локали надо делить символами, принятыми в стране, а не только пробелами
}
measureFormatted(msrm, old_unit, new_unit, fixed) {
return this._measureFormattedBase(msrm, old_unit, new_unit, fixed);
}
measureFormattedString(msrm, old_unit, new_unit, span_class, fixed) {
let result = this.measureFormatted(msrm, old_unit, new_unit, fixed);
if (result.length > 0) {
if (!Em.isEmpty(span_class) && !Em.isEmpty(result[1])) {
result[1] = `<span class=${span_class}>${result[1]}</span>`;
}
result[0] = this.dCapacityFormatted(result[0]);
result = result.join(' ');
}
return result;
}
numberFormattedByThousands(number, fixed = 1) {
return this._getNumber(number, 1000, fixed);
}
numberFormattedByMillions(number, fixed = 1) {
return this._getNumber(number, 1000000, fixed);
}
costFormattedObject(string, is_full_conversion, no_fraction = false) {
let msrm = parseFloat(string);
let absMsrm = Math.abs(msrm);
let value, text;
if (msrm === 0) {
value = msrm;
} else if (absMsrm < 1) {
value = msrm.toFixed(2);
} else if (absMsrm >= 10000) {
if (is_full_conversion) {
if (absMsrm < 1000000) {
value = this.numberFormattedByThousands(msrm);
text = Em.i18n.t('1000').toString();
} else if (absMsrm >= 1000000 && absMsrm < 1000000000) {
value = this.numberFormattedByMillions(msrm);
text = Em.i18n.t('1000000').toString();
} else if (absMsrm >= 1000000000) {
value = this._getNumber(msrm, 1000000000);
text = Em.i18n.t('1000000000').toString();
}
} else {
value = this.numberFormattedByThousands(msrm);
text = Em.i18n.t('1000').toString();
}
} else {
value = this._getNumber(msrm, 1, no_fraction ? 0 : 2, true);
}
return {
text,
value
};
}
costFormatted(string, is_full_conversion, no_fraction) {
let { text, value } = this.costFormattedObject(string, is_full_conversion, no_fraction);
if (text) {
return this.dCapacityFormatted(`${value} ${text}`);
} else {
return this.dCapacityFormatted(`${value}`);
}
}
costFormattedByMode(string, mode) {
let number = parseFloat(string);
let result;
switch (mode) {
case 'thousands':
result = this.numberFormattedByThousands(number, 2);
break;
case 'millions':
result = this.numberFormattedByMillions(number, 2);
break;
default:
result = this._getNumber(number, 1, 2);
}
return this.dCapacityFormatted(result);
}
costSize(string, is_full_conversion) {
let msrm = parseFloat(string);
if (msrm === 0) {
string = msrm;
} else if (msrm < 1) {
string = msrm.toFixed(2);
} else if (msrm >= 10000) {
if (!is_full_conversion) {
string = `${this.numberFormattedByThousands(msrm)}`;
} else {
if (msrm < 1000000) {
string = `${this.numberFormattedByThousands(msrm)}`;
} else if (msrm >= 1000000 && msrm < 1000000000) {
string = `${this.numberFormattedByMillions(msrm)}`;
} else if (msrm >= 1000000000) {
string = `${this._getNumber(msrm, 1000000000)}`;
}
}
} else {
string = this._getNumber(msrm, 1, 1);
}
return this.dCapacityFormatted(string);
}
costSizeString(string, is_full_conversion) {
let msrm = parseFloat(string);
if (msrm === 0) {
string = msrm;
} else if (msrm < 1) {
string = '';
} else if (msrm >= 10000) {
if (!is_full_conversion) {
string = `${Em.i18n.t('1000').toString()}`;
} else {
if (msrm < 1000000) {
string = `${Em.i18n.t('1000').toString()}`;
} else if (msrm >= 1000000 && msrm < 1000000000) {
string = `${Em.i18n.t('1000000').toString()}`;
} else if (msrm >= 1000000000) {
string = `${Em.i18n.t('1000000000').toString()}`;
}
}
} else {
string = '';
}
return string;
}
numFormatted(number, is_full_conversion) {
let msrm = parseFloat(number);
if (msrm < 1) {
number = {
value: msrm.toFixed(2),
txt: null
};
} else if (msrm >= 10000) {
if (!is_full_conversion) {
number = {
value: this.dCapacityFormatted(this.numberFormattedByThousands(msrm)),
txt: Em.i18n.t('1000').toString()
};
} else {
if (msrm < 1000000) {
number = {
value: this.dCapacityFormatted(this.numberFormattedByThousands(msrm)),
txt: Em.i18n.t('1000').toString()
};
} else if (msrm >= 1000000 && msrm < 1000000000) {
number = {
value: this.dCapacityFormatted(this.numberFormattedByMillions(msrm)),
txt: Em.i18n.t('1000000').toString()
};
} else if (msrm >= 1000000000) {
number = {
value: this.dCapacityFormatted(this._getNumber(msrm, 1000000000)),
txt: Em.i18n.t('1000000000').toString()
};
}
}
} else {
number = {
value: this.dCapacityFormatted(this._getNumber(msrm, 1, 2)),
txt: null
};
}
return number;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment