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;
    }
  • 相关阅读:
    How to import data from Oracle into PostgreSQL(转)
    C++——算法 回溯 八皇后问题
    Python——彩图变线稿
    算法——二叉树的遍历 前序 中序 后序 广度优先 深度优先 (转)
    C++——计数排序 (转)
    C++——位运算相关 (转)
    C++——std::vector相关 (转)
    C++——双指针 (转)
    C++——求三数之和,实操从低效做法逐步提升到高效做法,受益良多
    C++——基础容器
  • 原文地址:https://www.cnblogs.com/abeen/p/590407.html
Copyright © 2011-2022 走看看