0

我从 SQLLITE 数据库中获得了价格值。DB 中的数据类型为 REAL。

我正在获取值并将其放入使用 jQuery 的文本框中。

$(current_tr).find('.unit_price').val(row.unit_price);

此代码运行良好,没有任何错误,我可以在文本框中看到金额。但由于它的价格,我需要将其格式化为带 2 个小数位的价格。我从 Stackoverflow 中找到了一个自定义函数来执行此操作。

String.prototype.toPrice = function () {
    var v;
    if (/^\d+(,\d+)$/.test(this))
        v = this.replace(/,/, '.');
    else if (/^\d+((,\d{3})*(\.\d+)?)?$/.test(this))
        v = this.replace(/,/g, "");
    else if (/^\d+((.\d{3})*(,\d+)?)?$/.test(this))
        v = this.replace(/\./g, "").replace(/,/, ".");
    var x = parseFloat(v).toFixed(2).toString().split("."),
    x1 = x[0],
    x2 = ((x.length == 2) ? "." + x[1] : ".00"),
    exp = /^([0-9]+)(\d{3})/;
    while (exp.test(x1))
        x1 = x1.replace(exp, "$1" + "," + "$2");
    return x1 + x2;
}

该功能在以下条件下工作,没有任何错误。

$(current_tr).find('.unit_price').val('5000'.toPrice());

但我试图将它应用到它不返回任何东西的 db 值。文本框变得空白。下面的方法我已经累了。

$(current_tr).find('.unit_price').val(row.unit_price.toPrice());

$(current_tr).find('.unit_price').val((row.unit_price).toString().toPrice());

但它不适用于 DB 对象。我的方式有什么错误吗?

4

0 回答 0