Solved

Force a zero before decimal point using FomatCurrency

Posted on 2010-11-15
5
275 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
  • 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now