Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Excel到XML:将小数舍入到最接近的百分之一,并保留超链接_Javascript_Jquery_Html_Xml_Excel - Fatal编程技术网

Javascript Excel到XML:将小数舍入到最接近的百分之一,并保留超链接

Javascript Excel到XML:将小数舍入到最接近的百分之一,并保留超链接,javascript,jquery,html,xml,excel,Javascript,Jquery,Html,Xml,Excel,我随后将Excel电子表格导出为XML数据文件,然后在我的网站上将XML表显示为HTML表。效果很好。现在我“只”有一些我无法解决的小问题 (1) 输出表包含像1.325667这样的数字,但也包含很多0。我希望零显示为0.00,而带有许多小数的数字显示为1.33。基本上,每个数字都应该用两个小数来显示 (2) excel工作表包含指向我网站上其他页面的超链接,在呈现XML数据文件和HTML表时,我希望保留这些页面。到目前为止,这还不起作用。这可能吗 更新我找到了这个部分。通过将超链接拆分为字符串

我随后将Excel电子表格导出为XML数据文件,然后在我的网站上将XML表显示为HTML表。效果很好。现在我“只”有一些我无法解决的小问题

(1) 输出表包含像1.325667这样的数字,但也包含很多0。我希望零显示为0.00,而带有许多小数的数字显示为1.33。基本上,每个数字都应该用两个小数来显示

(2) excel工作表包含指向我网站上其他页面的超链接,在呈现XML数据文件和HTML表时,我希望保留这些页面。到目前为止,这还不起作用。这可能吗

更新我找到了这个部分。通过将超链接拆分为字符串,然后为这些字符串添加新列,然后调整源代码以包括

document.write("<tr><td><a href='");
document.write(x[i].getElementsByTagName("character-string")0].childNodes[0].nodeValue);
document.write(".php'>");
document.write(x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue);
document.write("</a></td>"); 
document.write(“”);
我能够包括超链接

Excel工作表的格式已经集成了这两个方面,但转换为XML文件似乎是个问题

非常感谢您的帮助(一次又一次:-)

更新我现在也找到了一种在Excel中进行四舍五入的方法,但我仍然坚持使用整数和只有一个小数的数字。基本上,我现在“只”需要一种方法来显示每个带两个小数点的数字,应用于整数(例如0应该是0.00)和带一个小数点的数字(例如1.5应该是1.50)。约翰尼里夫斯的答案似乎在正确的轨道上,但我无法让它发挥作用。还有其他想法吗

  • Number对象的方法为toFixed()

    1.325667.toFixed(2)=1.33

  • 在XML循环中运行时,选择URL并将其添加到链接:

    document.write(“)+”>)

    写一些文字

    文件。写(“
    ”)


  • Number.toFixed方法仅适用于浮点值(例如:2.1),而不适用于整数(例如:2或0)。您需要将数字类型转换为字符串类型,以便对其进行格式化以显示,并在不考虑输入的情况下获得一致的结果。类似这样的函数应该可以实现以下功能:

    function formatNumber(value) {
        var parts,
            trailingDigits,
            formattedRemainder;
    
        // Coerce the supplied value to a String type.
        value = String(value);
    
        // Break the supplied number into two parts, before and after the dot
        parts = value.split(".");
    
        // If there was no dot, there will only be one "part" and we can just
        // add the trailing zeros.
        if (parts.length === 1) {
            formattedRemainder = "00";
        }
        else {
            trailingDigits = parts[1];
    
            if (trailingDigits.length === 0) {
                // A dot, but no digits. (eg: 2. -> 2.00)
                formattedRemainder = "00";
            }
            else if (trailingDigits.length === 1) {
                // Add an extra trailing zero (eg: 2.1 -> 2.10)
                formattedRemainder = trailingDigits + "0";
            }
            else {
                // Just take the last two trailing digits.
                formattedRemainder = trailingDigits.substring(0, 2);
            }
        }
    
        // Build the final formatted string for display.
        return parts[0] + "." + formattedRemainder;
    }
    

    谢谢您的帮助。但是,我可能需要更多的指导:-)在这个代码字符串
    文档中的何处。写入(x[I].getElementsByTagName(“NAME”)[0].childNodes[0].nodeValue);
    我是否将
    添加到Fixed(2)中
    这样它会自动将每个数字舍入到小数点后第二位。我相信您解决超链接问题的方法也需要我为每一行插入链接,对吗?谢谢!很遗憾,我无法让它工作,但可能是因为我缺乏Web设计和XML方面的经验。我将您的代码放入了我的页面的页眉,但它不会更改表中的数字。我必须更改其他代码吗?您需要调用它,例如:
    document.write(formatNumber(x[I].getElementsByTagName(“名称”)[0].childNodes[0].nodeValue))
    我也试过了,但没有成功,它只是阻止表填充XML文件中的数据。