Solved

Force a zero before decimal point using FomatCurrency

Posted on 2010-11-15
5
301 Views
Last Modified: 2012-05-10
I have a very nice script that is formatting my numbers for me.   The problem arises if the person enters .5 instead 0.5 - I get NAN.5 on the results since starting with the decimal is throwing that out.    Can you help me update this so that if it starts with a decimal, the system will throw that 0 in for the user?   We are using the FormatCurrency section of the code.
<script>
 
var oldValue = [];
 
   var VK_BACK     = 0x08;      // BACKSPACE key
   var VK_TAB      = 0x09;      // TAB key 
   var VK_SHIFT    = 0x10;      // SHIFT key 
   var VK_CONTROL  = 0x11;      // CTRL key 
   var VK_MENU     = 0x12;      // ALT key 
   var VK_END      = 0x23;      // END key 
   var VK_HOME     = 0x24;      // HOME key 
   var VK_LEFT     = 0x25;      // LEFT ARROW key 
   var VK_RIGHT    = 0x27;      // RIGHT ARROW key 
   var VK_DELETE   = 0x2E;      // DEL key 
   var VK_0        = 0x30;      // 0 key
   var VK_1        = 0x31;      // 1 key
   var VK_2        = 0x32;      // 2 key
   var VK_3        = 0x33;      // 3 key
   var VK_4        = 0x34;      // 4 key
   var VK_5        = 0x35;      // 5 key
   var VK_6        = 0x36;      // 6 key
   var VK_7        = 0x37;      // 7 key
   var VK_8        = 0x38;      // 8 key
   var VK_9        = 0x39;      // 9 key
   var VK_NUMPAD0  = 0x60;      // Numeric keypad 0 key
   var VK_NUMPAD1  = 0x61;      // Numeric keypad 1 key
   var VK_NUMPAD2  = 0x62;      // Numeric keypad 2 key
   var VK_NUMPAD3  = 0x63;      // Numeric keypad 3 key
   var VK_NUMPAD4  = 0x64;      // Numeric keypad 4 key
   var VK_NUMPAD5  = 0x65;      // Numeric keypad 5 key
   var VK_NUMPAD6  = 0x66;      // Numeric keypad 6 key
   var VK_NUMPAD7  = 0x67;      // Numeric keypad 7 key
   var VK_NUMPAD8  = 0x68;      // Numeric keypad 8 key
   var VK_NUMPAD9  = 0x69;      // Numeric keypad 9 key
   var VK_SUBTRACT = 0x6D;      // Subtract key 
   var VK_DECIMAL  = 0x6E;      // Decimal key 
   var VK_HYPHEN   = 0xBD;      // Windows 2000/XP: For any country/region, the '-_' key
   var VK_PERIOD   = 0xBE;      // Windows 2000/XP: For any country/region, the '.>' key
 
   function formatCurrency(element, __event)
   {
      if (oldValue[element.id] == undefined)
      {
         oldValue[element.id] = "";
      }
      var keyCode;
      if (window.event) // IE
      {
         keyCode = __event.keyCode;
      }
      else if (__event.which) // Netscape/Firefox/Opera
      {
         keyCode = __event.which;
      }
      var shiftDown = __event.shiftKey;
      var ctrlDown  = __event.ctrlKey;
      var altDown   = __event.altKey;
      var validChar = 
      (
         (
            (
               (
                  ((keyCode >= VK_0)       && (keyCode <= VK_9))       || 
                  ((keyCode >= VK_NUMPAD0) && (keyCode <= VK_NUMPAD9))
               ) || 
               (
                  (keyCode == VK_HYPHEN)   || 
                  (keyCode == VK_SUBTRACT)
               ) || 
               (
                  (keyCode == VK_PERIOD)  || 
                  (keyCode == VK_DECIMAL)
               ) || 
               (
                  (keyCode == VK_DELETE) || 
                  (keyCode == VK_BACK)
               )
            ) && !shiftDown && !ctrlDown && !altDown
         ) || 
         (
            (
               (keyCode == VK_HOME)  || 
               (keyCode == VK_END)   || 
               (keyCode == VK_LEFT)  || 
               (keyCode == VK_RIGHT)
            )
         ) && !ctrlDown && !altDown
      );
      if (validChar)
      {
         if ((keyCode == VK_HYPHEN) || (keyCode == VK_SUBTRACT))
         {
            if (element.value.indexOf("-") != 0)
            {
               element.value = oldValue[element.id];
            }
            return false;
         }
         else if ((keyCode == VK_PERIOD) || (keyCode == VK_DECIMAL))
         {
            if (element.value.indexOf(".") < (element.value.length - 1))
            {
               element.value = oldValue[element.id];
            }
            return false;
         }
         else if ((keyCode == VK_HOME) || (keyCode == VK_END) || (keyCode == VK_LEFT) || (keyCode == VK_RIGHT) || (keyCode == VK_DELETE))
         {
            return false;
         }
		 else if (keyCode == VK_BACK)
         {
         }
      }
      else
      {
         if ((keyCode != VK_SHIFT) && (keyCode != VK_CONTROL) && (keyCode != VK_MENU) && (keyCode != VK_TAB))
         {
            element.value = oldValue[element.id];
         }
         return false;
      }
      var number     = moneyToNumber(element.value);
      var negative   = (number.substr(0, 1) == "-");
      number         = number.replace("-", "");
      var floatArray = number.split(".");
      floatArray[0]  = "" + parseFloat(floatArray[0]);
      var newValue   = addCommas(floatArray[0]);
      if (floatArray[1] != undefined)
      {
         newValue += "." + floatArray[1].substr(0, 2);
      }
      if (negative)
      {
         newValue = "-" + newValue;
      }
      if (element.value != newValue)
      {
         element.value        = newValue;
         oldValue[element.id] = newValue;
      }
      return false;
   }
 
   function addCommas(number)
   {
      return ((number.length > 3) ? (addCommas(number.substring(0, number.length - 3)) + "," + number.substring(number.length - 3, number.length)) : 
         String(number));
   }
 
   function moneyToNumber(money)
   {
      var number = money.replace("$", "");
      while (number.indexOf(",") > -1)
      {
         number = number.replace(",", "");
      }
	  if ((number.length)<1) return 0; else return number;
   }
</script>

Open in new window

0
Comment
Question by:digitalwise
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 14

Expert Comment

by:leoahmad
ID: 34136381
0
 
LVL 14

Expert Comment

by:leoahmad
ID: 34136383
0
 

Author Comment

by:digitalwise
ID: 34137039
That is helpful but I can't figure out where to add this to the script!
0
 
LVL 13

Accepted Solution

by:
dsmile earned 500 total points
ID: 34137342
This may be what you need,

Replace your current function moneyToNumber
function moneyToNumber(money)
   {
      var number = money.replace("$", "");
	  if (number.indexOf(".") == 0) {
		number = '0' + number;
	  }

      while (number.indexOf(",") > -1)
      {
         number = number.replace(",", "");
      }
          if ((number.length)<1) return 0; else return number;
   }

Open in new window

0
 

Author Closing Comment

by:digitalwise
ID: 34137563
Thank you!
0

Featured Post

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question