zoukankan      html  css  js  c++  java
  • [JavaScript] 通用数值格式化

    1、将正数转化为美元数值
    <html>
    <head>
     
    <title>Number Formatting</title>
     
    <script type="text/javascript">
      
    <!--
      
    // generic positive number decimal formatting function
      function format(expr, decplaces) {
       
    // raise incoming value by power of 10 times the
       // number of decimal places; round to an integer; convert to string
       var str = "" + Math.round(eval(expr) * Math.pow(10,decplaces));
       
    // pad small value strings with zeros to the left of rounded number
       while (str.length <= decplaces) {
        str 
    = "0" + str;
       }

       
    // establish location of decimal point
       var decpoint = str.length - decplaces;
       
    // assemble final result from: (a) the string up to the position of
       // the decimal point; (b) the decimal point; and (c) the balance
       // of the string. Return finished product.
       return str.substring(0,decpoint) + "." + 
       str.substring(decpoint,str.length);
      }

      
    // turn incoming expression into a dollar value
      function dollarize(expr) {
       
    return "$" + format(expr,2);
      }

      
    //-->
     </script>
    </head>
    <body>
     
    <h1>How to Make Money</h1>
     
    <form>
       Enter a positive floating point value or arithmetic expression to be
       converted to a currency format:
       
    <p><input type="text" name="entry" value="1/3" /> 
          
    <input type="button" value="&gt;Dollars and Cents&gt;" onclick="this.form.result.value=dollarize(this.form.entry.value)" />
          
    <input type="text" name="result" /></p>
     
    </form>
    </body>
    </html>

    2、将10进制转化成16进制
    function toHex(dec) {
     hexChars 
    = "0123456789ABCDEF";
     
    if (dec > 255{
      
    return null;
     }

     
    var i = dec % 16;
     
    var j = (dec - i) / 16;
     result 
    = "0X";
     result 
    += hexChars.charAt(j);
     result 
    += hexChars.charAt(i);
     
    return result;
    }
  • 相关阅读:
    李超线段树 (Li-Chao Segment Tree)
    NowCoder Contest 894
    AtCoder Beginning Contest 126
    华工软院IBM LinuxONE Community Cloud云计算实验文档
    Codeforces Round #561 (div. 2)
    Comet OJ Contest #3
    Codeforces Edu Round 65 (Rated for Div. 2)
    莫队算法 (Mo's Algorithm)
    Codeforces Round #559 (Div. 2)
    GDCPC2019 广东省赛总结
  • 原文地址:https://www.cnblogs.com/abeen/p/590407.html
Copyright © 2011-2022 走看看