Author Topic: Format price field  (Read 1533 times)

0 Members and 1 Guest are viewing this topic.

Offline casi

  • Jr. Member
  • **
  • Posts: 34
Format price field
« on: November 08, 2007, 01:17:38 PM »
Hi. Does anybody have some script to display a price or number field as following:

database: [[price]] value=1000000

web -> format field [[price]] from 1000000 to 1'000'000 for better reading...

Thanks Casi

Offline Jason

  • Administrator
  • Hero Member
  • *****
  • Posts: 1018
    • slipdex
Re: Format price field
« Reply #1 on: November 08, 2007, 02:41:23 PM »
Casi,

This really isn't a csv scripts question, it is more of a javascript question, but here is a script I have that will make notation for US currency and add cents.  You can play with it to modify it how you want:

Code: [Select]
<script>
function checkDecimal(obj, objStr){
    var objNumber;
    if(isNaN(objStr) && objStr!=''){
        alert('Value entered is not numeric');
        objNumber = '0.00';
    }
    else if(objStr==''){
        objNumber = '0.00';
    }
    else if(objStr.indexOf('.')!=-1){
        if(((objStr.length) - (objStr.indexOf('.')))>3){
            objStr = objStr.substr(0,((objStr.indexOf('.'))+3));
        }
        if(objStr.indexOf('.')==0){
            objStr = '0' + objStr;
        }
        var sLen = objStr.length;
        var TChar = objStr.substr(sLen-3,3);
        if(TChar.indexOf('.')==0){
            objNumber = objStr;
        }
        else if(TChar.indexOf('.')==1){
            objNumber = objStr + '0';
        }
        else if(TChar.indexOf('.')==2){
            objNumber = objStr + '00';
        }
    }
    else{
        objNumber = objStr + '.00';
    }
    obj.value = objNumber;
}
</script>

Then add this to your input field:

Code: [Select]
onBlur="checkDecimal(this,this.value)"

Jason
Founder of www.slipdex.com

Offline casi

  • Jr. Member
  • **
  • Posts: 34
Re: Format price field
« Reply #2 on: November 08, 2007, 03:24:41 PM »
Thanks Jason!

Offline casi

  • Jr. Member
  • **
  • Posts: 34
Re: Format price field
« Reply #3 on: November 08, 2007, 04:13:39 PM »
Jason

I found this here as well - works as well:

<html><head>
<script language="JavaScript">
function komma(nummer) {
var nummer = '' + nummer;
var laenge = nummer.length;
if (laenge> 3) {
var mod = laenge % 3;
var output = (mod> 0 ?
(nummer.substring(0,mod)) : '');

for (i=0 ; i < Math.floor(laenge / 3); i++) {
if ((mod == 0) && (i == 0))
output += nummer.substring(mod+ 3 * i,
mod + 3 * i + 3); else
output+= '.' + nummer.substring(mod + 3 * i,
mod + 3 * i + 3);} return (output);}
else return nummer; }
</script></head>

<body>
<script language="javascript">
document.write(komma(12345678)+'<br>');
document.write(komma(123456789)+'<br>');
document.write(komma(1234567890)+'<br>');
</script>
</body>
</html>

Cheers Casi