客户端javascript格式编号(货币)

时间:2016-08-11 20:57:20

标签: javascript xpages

想要以美元格式化货币字段。因此,如果用户输入14590.1,它将更改为14,590.10。我宁愿不用美元符号,但我目前的代码确实有这个。

我认为下面的内容并不错,虽然我也想检查一个有效的数字,如果无效,请将用户重新投入该字段。

我认为这很简单,并且会有一个功能可以做到这一点,但我找不到一个。

有什么比我扔在一起更好的东西吗?

<?xml version="1.0" encoding="UTF-8"?>
<xp:view
    xmlns:xp="http://www.ibm.com/xsp/core"
    xmlns:xe="http://www.ibm.com/xsp/coreex">
    <xp:scriptBlock
        id="scriptBlock2"
        type="text/javascript">
        <xp:eventHandler
            id="formatNumber"
            submit="false"
            event="onClientLoad">
        <xp:this.script><![CDATA[   
        formatNumber = function(x) {

                //Must remove $ and any commas
                y = x.replace(',','');
                z = y.replace('$','');

                //Must fix to 2 decimal places
                if ((typeof z) === 'string'){
                    z = parseFloat(z).toFixed(2)}
                else {
                    z = z.toFixed(2)
                }   

                //Now put it back in the field
                XSP.getElementById("#{id:djCurrencyTextBox1}").value = parseFloat(z);
                XSP.partialRefreshPost('#{id:djCurrencyTextBox1}');
            }
]]></xp:this.script>
        </xp:eventHandler>
    </xp:scriptBlock>
    <xp:inputText
        id="djCurrencyTextBox1"
        value="#{viewScope.a}"
        styleClass="pull-right"
        style="width:200px;text-align:right"
        defaultValue="0">
        <xp:this.converter>
            <xp:convertNumber type="currency"></xp:convertNumber>
        </xp:this.converter>
        <xe:this.dojoAttributes>
            <xp:dojoAttribute
                name="input"
                value="text-align: right">
            </xp:dojoAttribute>
        </xe:this.dojoAttributes>
        <xp:eventHandler
            event="onchange"
            submit="false">
            <xe:this.script><![CDATA[val = XSP.getElementById("#{id:djCurrencyTextBox1}").value
formatNumber(val);]]></xe:this.script>
        </xp:eventHandler>
    </xp:inputText>
</xp:view>

我尝试过使用dojo,但我不能让它接受一个值,如果用户没有输入两位数的分数。如果用户输入1234并且标签超出该字段,我希望将其修改为1234.00。如果他们输入1234.8并且标签应该是1234.80。我不认为我正在添加正确的属性:

<xe:djCurrencyTextBox
        id="djCurrencyTextBox2"
        value="#{doc.prjAmtColumn11}"
        styleClass="pull-right"
        style="width:100px;text-align:right"
        defaultValue="0">
        <xe:this.dojoAttributes>
            <xp:dojoAttribute
                name="input"
                value="text-align: right" />
            <xp:dojoAttribute
                name="constraints"
                value="fractional:yes">
            </xp:dojoAttribute>
            <xp:dojoAttribute
                name="places"
                value="2">
            </xp:dojoAttribute>
        </xe:this.dojoAttributes>
        <xe:this.constraints>
            <xe:djNumberConstraints
                currency="USD"
                fractional="auto"
                type="currency"
                places="2">
            </xe:djNumberConstraints>
        </xe:this.constraints>
        <xp:eventHandler
            event="onchange"
            submit="false">
            <xp:this.script><![CDATA[calculate();]]></xp:this.script>
        </xp:eventHandler>
    </xe:djCurrencyTextBox>

1 个答案:

答案 0 :(得分:0)

“Dojo货币文本框”控件执行您想要的操作,并包含在“控件”面板中“Dojo”窗体抽屉下的9.0.1中。

霍华德

if (rs != null) {
  do{
    sqlString = new StringBuffer();

    for (int i = 1; i <= columnCount; i++) {
        Object obj = rs.getObject(i);

        if (obj == null) {
            sqlString.append("null");
            sqlString.append(",");
        }
        else
        {
            int sqlTypes = rsmd.getColumnType(i);

            switch (sqlTypes) {
                case Types.VARCHAR:
                    sqlString.append("\"");
                    if (obj != null)
                        sqlString.append(rs.getString(i));

                    sqlString.append("\"");
                    sqlString.append(",");
                    break;
                case Types.NULL:
                    sqlString.append("null");
                    sqlString.append(",");
                    break;
                case Types.CHAR:
                    sqlString.append("\"");
                    if (obj != null)
                        sqlString.append(rs.getString(i));

                    sqlString.append("\"");
                    sqlString.append(",");
                    break;
                case Types.TIMESTAMP:
                    sqlString.append("\"");
                    if (obj != null)
                        sqlString.append(rs.getTimestamp(i));

                    sqlString.append("\"");
                    sqlString.append(",");
                    break;
                case Types.DOUBLE:
                    if (obj != null)
                        sqlString.append(rs.getDouble(i));

                    sqlString.append(",");
                    break;
                case Types.INTEGER:
                    if (obj != null)
                        sqlString.append(rs.getInt(i));

                    sqlString.append(",");
                    break;
                case Types.SMALLINT:
                    if (obj != null)
                        sqlString.append(rs.getInt(i));

                    sqlString.append(",");
                    break;
                case Types.DECIMAL:
                    if (obj != null)
                        sqlString.append(rs.getBigDecimal(i));

                    sqlString.append(",");
                    break;

                /*default:
                    if (obj != null)
                        sqlString.append(obj.toString());

                    sqlString.append(",");
                    break;*/
            }
        }while (rs.next());
    }