[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=">Dollars and Cents>" onclick="this.form.result.value=dollarize(this.form.entry.value)" />
<input type="text" name="result" /></p>
</form>
</body>
</html>
2、将10进制转化成16进制
<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=">Dollars and Cents>" 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;
}
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;
}