Google 计算器的输出似乎并不难解析。
我会做这样的事情:
//Parse JSON output to JS variables
ret = JSON.parse(JSON.stringify({lhs: "5 nanometers",rhs: "5.0 \x26#215; 10\x3csup\x3e-9\x3c/sup\x3e meters",error: "",icc: false} ));
//ret = Object {lhs: "5 nanometers", rhs: "5.0 × 10<sup>-9</sup> meters", error: "", icc: false}
var lhs = new Object();
//Check if lhs has exp notation
if (ret.lhs.search("× ")!==-1) {
//If so, conversion is a bit harder...but not much
temp = ret.lhs.split("× ");
lhs.base = temp[0]
temp = temp[1].split("<sup>")[1].split("</sup>");
lhs.exp = temp[0];
lhs.unit = temp[1];
lhs.num = parseFloat(lhs.base)*Math.pow(10,parseFloat(lhs.exp));
} else {
//If no, piece of cake
temp = ret.lhs.split(" ");
lhs.num = parseFloat(temp[0]);
lhs.unit = temp[1];
}
//Exactly the same for rhs
var rhs = new Object();
if (ret.rhs.search("× ")!==-1) {
temp = ret.rhs.split("× ");
rhs.base = temp[0]
temp = temp[1].split("<sup>")[1].split("</sup>");
rhs.exp = temp[0];
rhs.unit = temp[1];
rhs.num = parseFloat(rhs.base)*Math.pow(10,parseFloat(rhs.exp));
} else {
temp = ret.lhs.split(" ");
lhs.num = parseFloat(temp[0]);
lhs.unit = temp[1];
}
console.log("Converted",lhs.num,lhs.unit,"to",rhs.num,rhs.unit);
输出:将 5 纳米转换为 5e-9 米
由于您现在将 lhs 和 rhs 作为数字 javascript 变量并将它们的单位作为字符串,因此您可以随意格式化/处理它们。