Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript Code Currency Format, Help

Status
Not open for further replies.

sessiontr

Technical User
May 30, 2010
6
TR
this code is formatting the price like that 500 000 but i need to price format like this 500.000 so i need dot instead space

code at below
Code:
<html>
<head>
    <script type="text/javascript">

        window.onload = attachEvents;

        function attachEvents() {
            document.getElementById('myInput').onkeyup = reformatNumber;
        }

        function reformatNumber() {
            // No error checking. Assumes only ever 1 DP per number
            var text = this.value;

            // Strip off anything to the right of the DP
            var rightOfDp = '';
            var dpPos = text.indexOf('.');
            if (dpPos != -1) {
                rightOfDp = text.substr(dpPos);
                text = text.substr(0, dpPos);
            }

            var leftOfDp = '';
            var counter = 0;
            // Format the remainder into 3 char blocks, starting from the right
            for (var loop=text.length-1; loop>-1; loop--) {
                var char = text.charAt(loop);

                // Ignore existing spaces
                if (char == ' ') continue;

                leftOfDp = char + leftOfDp;
                counter++;
                if (counter % 3 == 0) leftOfDp = ' ' + leftOfDp;
            }

            // Strip leading space if present
            if (leftOfDp.charAt(0) == ' ') leftOfDp = leftOfDp.substr(1);

            this.value = leftOfDp + rightOfDp;

        }

    </script>
</head>

<body>
    <form>
        <input type="text" id="myInput" />
    </form>
</body>
</html>
 
Do you normally post code on public forums, and then privately email people the link in a vain hope of getting a faster reply?

Or did you single me out for a reason?

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Snippets & Info:
The Out Atheism Campaign
 
you shared this code on this forum, for that reason i asked that question generally public forum but nobody helps me, so i thought that, you know the best answer because you shared. and i sent you public mail :))
confused :S
 
firstly asked on this forum and then i just sent you privately email.
 
Not sure exactly what it is you want, as your code maintains pretty much any periods it finds. but of what you want is to have a period between each set of 3 numbers then just add a period in the following line

if (counter % 3 == 0) leftOfDp = [red]'.'[/red] + leftOfDp;

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
thanks vacunita but not work,
normally it is putting space like that

100 000 000
10 000

but i dont want space, i want dot like that

100.000.000
10.000
 
That's exactly what my alteration does, Did you try it?

I tested this and it works.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
i tried but it makes like that .100000

maybe i can't do

can you paste all modificated code here?
 
O.k, here you were right it wasn't working, try this then:

Code:
<html>
<head>
    <script type="text/javascript">

        window.onload = attachEvents;

        function attachEvents() {
            document.getElementById('myInput').onkeyup = reformatNumber;
        }

        function reformatNumber() {
            // No error checking. Assumes only ever 1 DP per number
            var text = this.value;
            [red]text=text.replace(/\./g," ");[/red]
            // Strip off anything to the right of the DP
            var rightOfDp = '';
            //var dpPos = text.indexOf('.');
            //if (dpPos != -1) {
             //   rightOfDp = text.substr(dpPos);
               // text = text.substr(0, dpPos);
            //}

            var leftOfDp = '';
            var counter = 0;
            // Format the remainder into 3 char blocks, starting from the right
            for (var loop=text.length-1; loop>-1; loop--) {
                var char = text.charAt(loop);

                // Ignore existing spaces
                if (char == ' ') continue;

                leftOfDp = char + leftOfDp;
                counter++;
                if (counter % 3 == 0) leftOfDp = ' ' + leftOfDp;
            }

            // Strip leading space if present
            if (leftOfDp.charAt(0) == ' ') leftOfDp = leftOfDp.substr(1);
            [red]leftOfDp=leftOfDp.replace(/ /g,".");[/red]
            this.value = leftOfDp + rightOfDp;


        }

    </script>
</head>

<body>
    <form>
        <input type="text" id="myInput" />
    </form>
</body>
</html>





----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
vacunita, thank you very much, it is working perfect now,
you are really good programmer.
thanks again.
 
Glad I could help.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top