java 具有货币格式的 JSF convertNumber 是否舍入或截断额外的小数位?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/379027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does the JSF convertNumber with currency format round or truncate additional decimal places?
提问by s_t_e_v_e
Here is an example:
下面是一个例子:
<h:outputText value="#{myBean.myMoney}">
<f:convertNumber type="currency" currencySymbol="$" />
</h:outputText>
Given that I have $1.006, will this output $1.00 or $1.01?
鉴于我有 1.006 美元,这个输出是 1.00 美元还是 1.01 美元?
Doesn't say here: http://java.sun.com/javaee/javaserverfaces/1.1_01/docs/tlddocs/f/convertNumber.html
这里不说:http: //java.sun.com/javaee/javaserverfaces/1.1_01/docs/tlddocs/f/convertNumber.html
回答by s_t_e_v_e
Answer=Rounded
答案=四舍五入
Hmmm....does that sound right? I don't think its a good idea to be rounding up money. Hopefully no banking apps are going to rely on this one.
嗯……听起来对吗?我不认为把钱凑齐是个好主意。希望没有银行应用程序会依赖这个。
Brings to mind this scene from One Flew Over the Cuckoo's Nest...
让人想起《飞越杜鹃巢》中的这一幕……
[the inmates are playing cards and betting with cigarettes]
Martini: [rips a cigarette in half] I bet a nickel.
McMurphy: Dime's the limit, Martini.
Martini: I bet a dime.
[Puts the two halves onto the table]
McMurphy: This is not a dime, Martini. This is a dime.
[shows a whole cigarette]
McMurphy: If you break it in half, you don't get two nickels, you get shit. Try and smoke it. You understand?
Martini: Yes.
McMurphy: You don't understand.
[囚犯们在打牌并用香烟打赌]
Martini:[将一根香烟撕成两半]我赌五分钱。
麦克墨菲:一角钱是极限,马提尼。
马提尼:我打赌一角钱。
[把两半放在桌子上]
麦克墨菲:这不是一角钱,马提尼。这是一毛钱。
[
拿出一整根香烟] McMurphy:如果你把它掰成两半,你不会得到两枚硬币,你得到的是狗屎。试着抽烟。你明白?
马提尼:是的。
麦克墨菲:你不明白。
回答by Warrior
Currency can be set only to 2 decimal places, setting it to more than 2 decimal places is wrong. If the input is with more than 2 decimal places the output would be truncated to 2 decimal places.
货币只能设置2位小数,设置超过2位小数是错误的。如果输入的小数位超过 2 位,则输出将被截断为 2 位小数。
Logically $1.001 would not make any sense as $1 and 1 cent would be $1.01 not $1.001.
从逻辑上讲,1.001 美元没有任何意义,因为 1 美元和 1 美分是 1.01 美元而不是 1.001 美元。

