使用Angular

时间:2017-03-03 16:32:46

标签: angularjs

这可能是一个愚蠢的问题,但我在Angular很新。如何格式化float / double以仅在分隔符后显示两位小数。

 <input type="text" name="whatever" value="{{model.Rate}}" />

我在SO中看到了一些question,但我相信这样的基本操作可以用更简单的方式完成。

3 个答案:

答案 0 :(得分:2)

尝试这样的事情

struct TokenHandler
{
    int tokenIdx;
    eventDataStatus_t (Schema::*fpObjHandler)(MessageData &md, EventDataImpl &evd, int &tokenIdx);
};

Schema::event(MessageData &md, EventDataImpl &evd)
{
    int tokenIdx = 0;
    std::map<std::string, TokenHandler> tokenHandlerMap;
    tokenHandlerMap["timeInterval"] = { -1, &Schema::timeInterval };

    // ..
    // ....

    TokenHandler th = tokenHandlerMap.at(key);
    if (th.fpObjHandler != NULL) {
        th.fpObjHandler(md, evd, tokenIdx); // THIS RESULTS IN ERROR
        //..
        //...
    }
}

eventDataStatus_t Schema::timeInterval(MessageData &md, EventDataImpl &evd, int &tokenIdx)
{
    //..
    //...
    return OK;
}

假设<input type="text" name="whatever" value="{{model.Rate.toFixed(2)}} /> 的类型为Rate

toFixed()返回一个字符串,并始终用零填充,因此number

答案 1 :(得分:2)

Angulars有一些内置过滤器可以做到这一点。

Angular doc

在你的情况下,这应该是这样的:

value="{{ number_expression | number : 2}}"

答案 2 :(得分:0)

对于角度8,请使用: {{number_expression |数字:“ 1.2-2”}}。 小数点前至少一位,最大小数点前两位。