更新单位转换函数

This commit is contained in:
Terry Cai 2019-01-26 17:42:21 +08:00 committed by GitHub
parent 5a78e68a26
commit e8a9aa275c

View File

@ -235,4 +235,28 @@ function setStringPrototype() {
}
return res;
};
String.prototype.toRpx = function (minus) {
let reg;
if (minus) {
reg = /^-?[0-9]+([.]{1}[0-9]+){0,1}(rpx|px)$/g;
} else {
reg = /^[0-9]+([.]{1}[0-9]+){0,1}(rpx|px)$/g;
}
const results = reg.exec(this);
if (!this || !results) {
console.error(`The size: ${this} is illegal`);
return 0;
}
const unit = results[2];
const value = parseFloat(this);
let res = 0;
if (unit === 'rpx') {
res = Math.round(value);
} else if (unit === 'px') {
res = Math.round(value / screenK);
}
return res;
}
}