• JS代码格式化修改表格的数值的格式


    今天在cognos中第一次需要用到JS,主要是报表页面展示的时候是可能得到如下的数据

    ,我需要对其中类型中有金额字样的,后面的数值,精确2位小数;有百分比字样的,数值显示成百分比。如下

    我先尝试了自己写一段HTML代码,对表格的数据进行格式化。

    <html>
    <head>
    <script type="text/javascript">
    function getInnerHTML()
    {
    alert(document.getElementById("mytable").innerHTML);
    }

    window.onload=function(){

    formatMoney();

    }


    function formatMoney(){
    var fisrTd = document.getElementsByTagName("td");
    var i=0;
    for(i=0;i<fisrTd.length;i=i+2){
    if(fisrTd[i].innerHTML.indexOf("百分比")>=0){
    alert(fisrTd[i].innerHTML)
    fisrTd[i+1].innerHTML=accMul(fisrTd[i+1].innerHTML,100)+"%"
    }
    if(fisrTd[i].innerHTML.indexOf("金额")>=0){
    alert(fisrTd[i].innerHTML)
    fisrTd[i+1].innerHTML=outputmoney(fisrTd[i+1].innerHTML)
    }

    }

    }

    //乘法函数,用来得到精确的乘法结果
    //说明:javascript的乘法结果会有误差,在两个浮点数相乘的时候会比较明显。这个函数返回较为精确的乘法结果。
    //调用:accMul(arg1,arg2)
    //返回值:arg1乘以 arg2的精确结果
    function accMul(arg1,arg2)
    {
    var m=0,s1=arg1.toString(),s2=arg2.toString();
    try{m+=s1.split(".")[1].length}catch(e){}
    try{m+=s2.split(".")[1].length}catch(e){}
    return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m)
    }

    function outputmoney(number) {
    number = number.replace(/,/g, "");
    if(isNaN(number) || number == "")
    return "";
    number = Math.round(number * 100) / 100;
    if (number < 0)
    return '-' + outputdollars(Math.floor(Math.abs(number) - 0) + '') + outputcents(Math.abs(number) - 0);
    else
    return outputdollars(Math.floor(number - 0) + '') + outputcents(number - 0);
    }
    //格式化金额
    function outputdollars(number) {
    if (number.length <= 3)
    return (number == '' ? '0' : number);
    else {
    var mod = number.length % 3;
    var output = (mod == 0 ? '' : (number.substring(0, mod)));
    for (i = 0; i < Math.floor(number.length / 3); i++) {
    if ((mod == 0) && (i == 0))
    output += number.substring(mod + 3 * i, mod + 3 * i + 3);
    else
    output += ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);
    }
    return (output);
    }
    }
    function outputcents(amount) {
    amount = Math.round(((amount) - Math.floor(amount)) * 100);
    return (amount < 10 ? '.0' + amount : '.' + amount);
    }


    </script>
    </head>
    <body>

    <table border="1" id="mytable">
    <tr id="tr1">
    <td >类型</td>
    <td>数值</td>
    </tr>
    <tr id="tr2">
    <td >金额</td>
    <td>10.1</td>
    </tr>
    <tr id="tr3">
    <td >人数</td>
    <td>100</td>
    </tr>
    <tr id="tr4">
    <td >百分比</td>
    <td>1</td>
    </tr>
    <tr id="tr5">
    <td >总金额</td>
    <td>12.12563</td>
    </tr>
    </table>

    <input type="button" onclick="getInnerHTML()"
    value="Alert innerHTML of table row" />

    </body>
    </html>

    然后在cognos中,先给TABLE嵌套一个DIV,然后通过DIV的ID,获取到表格,再根据用以上JS代码去实现我需要的功能:

    <script type="text/javascript">

    setTimeout(formatMoney,20)

    function formatMoney(){
    var tabObj = document.getElementById("div1");
    var fisrTd = tabObj.firstChild.getElementsByTagName("td")
    var i=0;
    for(i=0;i<fisrTd.length;i=i+1){
    if(fisrTd[i].innerHTML.indexOf("率")>=0){
    //alert(fisrTd[i].innerText)
    fisrTd[i+1].innerText=accMul(fisrTd[i+1].innerText,100)+"%"
    }
    if(fisrTd[i].innerHTML.indexOf("占比")>=0){
    //alert(fisrTd[i].innerText)
    fisrTd[i+1].innerText=accMul(fisrTd[i+1].innerText,100)+"%"
    }

    if(fisrTd[i].innerHTML.indexOf("额")>=0){
    //alert(fisrTd[i].innerText)
    fisrTd[i+1].innerText=outputmoney(fisrTd[i+1].innerText)
    }

    }

    }

    /* 乘法函数,用来得到精确的乘法结果
    说明:javascript的乘法结果会有误差,在两个浮点数相乘的时候会比较明显。这个函数返回较为精确的乘法结果。
    调用:accMul(arg1,arg2)
    返回值:arg1乘以 arg2的精确结果
    */
    function accMul(arg1,arg2)
    {
    var m=0,s1=arg1.toString(),s2=arg2.toString();
    try{m+=s1.split(".")[1].length}catch(e){}
    try{m+=s2.split(".")[1].length}catch(e){}
    return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m)
    }

    function outputmoney(number) {
    number = number.replace(/,/g, "");
    if(isNaN(number) || number == "")return "";
    number = Math.round(number * 100) / 100;
    if (number < 0)
    return '-' + outputdollars(Math.floor(Math.abs(number) - 0) + '') + outputcents(Math.abs(number) - 0);
    else
    return outputdollars(Math.floor(number - 0) + '') + outputcents(number - 0);
    }
    /*格式化金额
    */
    function outputdollars(number) {
    if (number.length <= 3)
    return (number == '' ? '0' : number);
    else {
    var mod = number.length % 3;
    var output = (mod == 0 ? '' : (number.substring(0, mod)));
    for (i = 0; i < Math.floor(number.length / 3); i++) {
    if ((mod == 0) && (i == 0))
    output += number.substring(mod + 3 * i, mod + 3 * i + 3);
    else
    output += ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);
    }
    return (output);
    }
    }
    function outputcents(amount) {
    amount = Math.round(((amount) - Math.floor(amount)) * 100);
    return (amount < 10 ? '.0' + amount : '.' + amount);
    }

    </script>

  • 相关阅读:
    一个关于java线程的面试题
    【Feature】初探Feature
    Foreign Keys in the Entity Framework
    JS keycode
    SQLyog8.3 . 8.4 Enterprise/Ultimate crack
    Win7下使用toad连接oracle出现can't initialize OCI 1
    ADO 数据类型转换表
    简单Jscript(ASP)模版操作文件
    自适应宽度的左右结构DIV+CSS
    一个比较好用的 classic asp Jscript 框架 SmartAsp
  • 原文地址:https://www.cnblogs.com/longjshz/p/4655074.html
Copyright © 2020-2023  润新知