格式编号到价格 [英] format number to price

查看:82
本文介绍了格式编号到价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了一些这样的jquery事情,只是想知道是否有一个简单的数字格式化脚本。

I have seen a few of these jquery things going on, and just wondered if there was a simple number formatting script.

基本上,我们所希望做的一切,格式(客户端)仅用于检查目的,在字段中输入的数字。要显示页面上的其他位置(可能是div),他们输入的格式化价格。

Essentially, all we wish to do, is format ( client side ) for checking purposes only, the number entered in a field. To show somewhere else on the page ( presumably in a div ) the formatted price they entered.

所以我们可以说,字段

input id="price" name="price" size="50" type="text" class="medium" /

他们输入1234560

And they enter 1234560

我想在页面上的其他地方显示:
您输入:$ 1,234,560.00作为价格。这是正确的吗?

I want to show somewhere else on the page, : You Entered : $1,234,560.00 as the price. Is this correct ?

这仅用于视觉目的。或者,更改它们键入的值并将其格式化为实时可能是一个选项,但是我们要发送到数据库的值是纯数字,即:1234560

This is only for visual purposes only. Alternatively, changing the value of what they type and formatting it "live" could be an option, however the value we want to send to the db is pure numerics, ie: 1234560

推荐答案

设置这样的函数
Javascript格式货币

function CurrencyFormatted(amount)
{
    var i = parseFloat(amount);
    if(isNaN(i)) { i = 0.00; }
    var minus = '';
    if(i < 0) { minus = '-'; }
    i = Math.abs(i);
    i = parseInt((i + .005) * 100);
    i = i / 100;
    s = new String(i);
    if(s.indexOf('.') < 0) { s += '.00'; }
    if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
    s = minus + s;
    return s;
}

然后为这样的jQuery设置onchange

Then set an onchange for jQuery something like this

jQuery(document).ready(function(){

  jQuery('#price').change(function(){

      jQuery('#mydivsomewhere').val(CurrencyChange(jQuery('#price').val));
  });

});

不确定这是否100%正确,尚未测试过。但是,只要输入框中的文本发生变化,就应该调用CurrencyFormat。应传入id为price的文本框的val,并使用格式化的值设置id为mydivsomewhere的div。

Not sure if that is 100% correct, haven't tested it. But should call CurrencyFormat whenever the text in your input box changes. Should pass in the val of the textbox with id of price and set a div of id mydivsomewhere with the formatted value.

这篇关于格式编号到价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆