title: Calculating the maximum value for a decimal using scale and precision
body: <p>I am working on a JavaScript function that takes two values:  precision of a decimal value &amp; scale of a decimal value.</p>
  
  <p>This function should calculate the maximum value that can be stored in a decimal of that size.</p>
  
  <p>For example:  a decimal with a precision of 5 and a scale of 3 would have a maximum value of 99.999.</p>
  
  <p>What I have does the job, but it's not elegant.  Can anyone think of something more clever?</p>
  
  <p>Also, please forgive the use of this weird version of Hungarian notation.</p>
  
  <pre><code>function maxDecimalValue(pintPrecision, pintScale) {
      /* the maximum integers for a decimal is equal to the precision - the scale.
          The maximum number of decimal places is equal to the scale.
          For example, a decimal(5,3) would have a max value of 99.999
      */
      // There's got to be a more elegant way to do this...
      var intMaxInts = (pintPrecision- pintScale);
      var intMaxDecs = pintScale;
  
      var intCount;
      var strMaxValue = "";
  
      // build the max number.  Start with the integers.
      if (intMaxInts == 0) strMaxValue = "0";    
      for (intCount = 1; intCount &lt;= intMaxInts; intCount++) {
          strMaxValue += "9";
      }
  
      // add the values in the decimal place
      if (intMaxDecs &gt; 0) {
          strMaxValue += ".";
          for (intCount = 1; intCount &lt;= intMaxDecs; intCount++) {
              strMaxValue += "9";
          }
      }
      return parseFloat(strMaxValue);
  }
  </code></pre>