<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF8">
<script>
function canmaPrint(str, digit) {
// カンマを削除
str = str.split(",").join("");
// 先頭一桁が符号の場合は保持する
hugou = "";
if ('+'== str.charAt(0) || '-'== str.charAt(0)) {
hugou = str.charAt(0);
str = str.slice(1);
}
// 文字列を数値に変えて、小数末尾の0を削除
str = String(Number(str));
// 整数と小数を分割
tmpstr = str.split(".");
seisuu = tmpstr[0];
shousuu = "";
if (tmpstr.length >= 2) {
shousuu = tmpstr[1];
}
// 小数点桁数を取得
// digitの方が大きかったら差の分だけ0を付ける
zeroNum = digit - shousuu.length
zeroStr = "";
for (i = 0; i < zeroNum; i++) {
zeroStr = zeroStr + "0";
}
shousuu = shousuu + zeroStr;
// 整数をカンマ編集
for(i = 0; i < seisuu.length/3; i++){
seisuu = seisuu.replace(/^([+-]?\d+)(\d\d\d)/,"$1,$2");
}
outStr = seisuu;
if (shousuu.length != 0) {
outStr = outStr + "." + shousuu;
}
return hugou + outStr;
// document.write("### " + zeroStr + " ###");
//数値xと表示小数点桁数y
//xの小数点桁数とyの多い方分の桁すうだけ0埋めして返す。
//xの小数点桁数は、末尾の0は除いた場合の数になる
// 例)1.10 <ー小数点桁数1
//返す値は、カンマ編集されている。
//xには、カンマ編集されている場合がある。
//xには、正負(+,-)の符号が付いている場合がある
//yには、0~4の値が設定される(もっと増えてもよい)
}
function test(str, digit, kitai) {
kekkaStr = canmaPrint(str, digit);
kekkaMaru = "×";
if (kitai == kekkaStr) {
kekkaMaru = "○";
}
document.write(
str + ":" +
digit + ":" +
kitai + ":" +
kekkaStr + ":" +
kekkaMaru +
"<br/>");
}
</script>
</head>
<body>
</body>
<script>
document.write("JavaScript<br/>");
// 問題
//数値xと表示小数点桁数y
//xの小数点桁数とyの多い方分の桁すうだけ0埋めして返す。
//xの小数点桁数は、末尾の0は除いた場合の数になる
// 例)1.10 <ー小数点桁数1
//返す値は、カンマ編集されている。
//xには、カンマ編集されている場合がある。
//xには、正負(+,-)の符号が付いている場合がある
//yには、0~4の値が設定される(もっと増えてもよい)
//パターン
test("0", "0", "0" );
test("0.1", "0", "0.1" );
test("0.10", "0", "0.1" );
test("0.010", "0", "0.01" );
test("0", "1", "0.0" );
test("0.1", "1", "0.1" );
test("0.10", "1", "0.1" );
test("0.010", "1", "0.01" );
test("0", "2", "0.00" );
test("0.1", "2", "0.10" );
test("0.10", "2", "0.10" );
test("0.010", "2", "0.01" );
test("1,000", "0", "1,000" );
test("1,000.1", "0", "1,000.1" );
test("1,000.10", "0", "1,000.1" );
test("1,000.010", "0", "1,000.01" );
test("1,000", "1", "1,000.0" );
test("1,000.1", "1", "1,000.1" );
test("1,000.10", "1", "1,000.1" );
test("1,000.010", "1", "1,000.01" );
test("1,000", "2", "1,000.00" );
test("1,000.1", "2", "1,000.10" );
test("1,000.10", "2", "1,000.10" );
test("1,000.010", "2", "1,000.01" );
test("1,001,000", "0", "1,001,000" );
test("1,001,000.1", "0", "1,001,000.1" );
test("1,001,000.10", "0", "1,001,000.1" );
test("1,001,000.010", "0", "1,001,000.01" );
test("1,001,000", "1", "1,001,000.0" );
test("1,001,000.1", "1", "1,001,000.1" );
test("1,001,000.10", "1", "1,001,000.1" );
test("1,001,000.010", "1", "1,001,000.01" );
test("1,001,000", "2", "1,001,000.00" );
test("1,001,000.1", "2", "1,001,000.10" );
test("1,001,000.10", "2", "1,001,000.10" );
test("1,001,000.010", "2", "1,001,000.01" );
test("+0.1", "0", "+0.1" );
test("+0.10", "0", "+0.1" );
test("+0.010", "0", "+0.01" );
test("+0.1", "1", "+0.1" );
test("+0.10", "1", "+0.1" );
test("+0.010", "1", "+0.01" );
test("+0.1", "2", "+0.10" );
test("+0.10", "2", "+0.10" );
test("+0.010", "2", "+0.01" );
test("+0.1", "0", "+0.1" );
test("+0.11", "0", "+0.11" );
test("+0.111", "0", "+0.111" );
test("+0.1111", "0", "+0.1111" );
test("+0.11111", "0", "+0.11111" );
test("+0.01", "0", "+0.01" );
test("+0.011", "0", "+0.011" );
test("+0.0111", "0", "+0.0111" );
test("+0.01111", "0", "+0.01111" );
test("+0.011111", "0", "+0.011111" );
test("+0.01", "1", "+0.01" );
test("+0.011", "1", "+0.011" );
test("+0.0111", "1", "+0.0111" );
test("+0.01111", "1", "+0.01111" );
test("+0.011111", "1", "+0.011111" );
test("+0.10", "0", "+0.1" );
test("+0.110", "0", "+0.11" );
test("+0.1110", "0", "+0.111" );
test("+0.11110", "0", "+0.1111" );
test("+0.111110", "0", "+0.11111" );
test("+1,000", "0", "+1,000" );
test("+1,000.1", "0", "+1,000.1" );
test("+1,000.10", "0", "+1,000.1" );
test("+1,000.010", "0", "+1,000.01" );
test("+1,000", "1", "+1,000.0" );
test("+1,000.1", "1", "+1,000.1" );
test("+1,000.10", "1", "+1,000.1" );
test("+1,000.010", "1", "+1,000.01" );
test("+1,000", "2", "+1,000.00" );
test("+1,000.1", "2", "+1,000.10" );
test("+1,000.10", "2", "+1,000.10" );
test("+1,000.010", "2", "+1,000.01" );
test("-1,001,000", "0", "-1,001,000" );
test("-1,001,000.1", "0", "-1,001,000.1" );
test("-1,001,000.10", "0", "-1,001,000.1" );
test("-1,001,000.010", "0", "-1,001,000.01" );
test("-1,001,000", "1", "-1,001,000.0" );
test("-1,001,000.1", "1", "-1,001,000.1" );
test("-1,001,000.10", "1", "-1,001,000.1" );
test("-1,001,000.010", "1", "-1,001,000.01" );
test("-1,001,000", "2", "-1,001,000.00" );
test("-1,001,000.1", "2", "-1,001,000.10" );
test("-1,001,000.10", "2", "-1,001,000.10" );
test("-1,001,000.010", "2", "-1,001,000.01" );
test("1,001,001,000", "0", "1,001,001,000" );
test("1,001,001,000.1", "0", "1,001,001,000.1" );
test("1,001,001,000.10", "0", "1,001,001,000.1" );
test("1,001,001,000.010", "0", "1,001,001,000.01" );
test("1,001,001,000", "1", "1,001,001,000.0" );
test("1,001,001,000.1", "1", "1,001,001,000.1" );
test("1,001,001,000.10", "1", "1,001,001,000.1" );
test("1,001,001,000.010", "1", "1,001,001,000.01" );
test("1,001,001,000", "2", "1,001,001,000.00" );
test("1,001,001,000.1", "2", "1,001,001,000.10" );
test("1,001,001,000.10", "2", "1,001,001,000.10" );
test("1,001,001,000.010", "2", "1,001,001,000.01" );
test("1,001,001,000", "3", "1,001,001,000.000" );
test("1,001,001,000.1", "3", "1,001,001,000.100" );
test("1,001,001,000.10", "3", "1,001,001,000.100" );
test("1,001,001,000.010", "3", "1,001,001,000.010" );
test("1,001,001,000", "4", "1,001,001,000.0000" );
test("1,001,001,000.1", "4", "1,001,001,000.1000" );
test("1,001,001,000.10", "4", "1,001,001,000.1000" );
test("1,001,001,000.010", "4", "1,001,001,000.0100" );
test("1,001,001,000", "5", "1,001,001,000.00000" );
test("1,001,001,000.1", "5", "1,001,001,000.10000" );
test("1,001,001,000.10", "5", "1,001,001,000.10000" );
test("1,001,001,000.010", "5", "1,001,001,000.01000" );
document.write("JavaScript End<br/>");
</script>
</html>