JavaScript四捨五入の基本 - Math.roundの使い方を初心者向けに解説

JavaScriptのMath.roundで四捨五入を実装する方法を初心者向けに詳しく解説。小数点以下の処理、切り上げ・切り捨てとの違い、実践的な活用例まで具体的なコード例とともに紹介します。

Learning Next 運営
25 分で読めます

JavaScript四捨五入の基本 - Math.roundの使い方を初心者向けに解説

JavaScriptで数値を扱っていると、四捨五入で悩んだことはありませんか?

「価格計算で小数点が出てしまう」 「Math.roundの正確な動作が分からない」

そんな疑問を抱いている方は多いと思います。 でも大丈夫です!

この記事では、JavaScriptのMath.roundを使った四捨五入の基本から実践までを詳しく解説します。 基本的な使い方から実際の開発で役立つテクニックまで、具体的なコード例とともにご紹介していきます。

きっと「こんなに便利だったんだ!」と感じられるはずですよ。

Math.roundの基本を理解しよう

Math.roundって何?

Math.roundは、JavaScriptで四捨五入を行うメソッドです。

簡単に言うと、小数点以下第1位を基準に数値を整数に丸める機能です。 0.5以上なら切り上げ、0.5未満なら切り捨てを行います。

まずは基本的な動作を見てみましょう。

// Math.roundの基本的な使い方
console.log(Math.round(4.7)); // 5(0.7 >= 0.5なので切り上げ)
console.log(Math.round(4.4)); // 4(0.4 < 0.5なので切り捨て)
console.log(Math.round(4.5)); // 5(0.5なので切り上げ)

この例では、小数点以下の値に応じて適切に四捨五入されています。 4.75 に、4.44 に、4.55 になりますね。

負数での動作も確認しよう

負数の場合も同じルールで動作します。

console.log(Math.round(-4.5)); // -4(負数の場合も同様)
console.log(Math.round(-4.7)); // -5(負数の切り上げ)
console.log(Math.round(-4.3)); // -4(負数の切り捨て)
// 整数の場合はそのまま
console.log(Math.round(10)); // 10
console.log(Math.round(0)); // 0

負数の-4.5-4になることに注目してください。 これは0に近い方向に丸められるためです。

型変換の動作を理解しよう

Math.roundは数値以外の値も自動的に変換して処理します。

// 文字列の自動変換
console.log(Math.round('4.7')); // 5(文字列は自動的に数値に変換)
console.log(Math.round('hello')); // NaN(変換できない文字列)
console.log(Math.round('')); // 0(空文字列は0に変換)
// 特殊な値の場合
console.log(Math.round(null)); // 0(nullは0に変換)
console.log(Math.round(undefined)); // NaN(undefinedは変換不可)

文字列は自動的に数値に変換されますが、無効な文字列はNaNになります。 これらの動作を理解しておくと、エラーの原因が分かりやすくなりますね。

他の数値処理メソッドとの違いを知ろう

Math.ceil(切り上げ)との比較

Math.ceilは常に小数点以下を切り上げる関数です。

console.log('=== Math.round vs Math.ceil ===');
// 四捨五入と切り上げの比較
console.log('値: 4.1');
console.log(' Math.round:', Math.round(4.1)); // 4
console.log(' Math.ceil:', Math.ceil(4.1)); // 5
console.log('値: 4.9');
console.log(' Math.round:', Math.round(4.9)); // 5
console.log(' Math.ceil:', Math.ceil(4.9)); // 5

四捨五入では4.14になりますが、切り上げでは5になります。 用途に応じて使い分けることが大切ですね。

Math.floor(切り捨て)との比較

Math.floorは常に小数点以下を切り捨てる関数です。

console.log('=== Math.round vs Math.floor ===');
// 四捨五入と切り捨ての比較
console.log('値: 4.7');
console.log(' Math.round:', Math.round(4.7)); // 5
console.log(' Math.floor:', Math.floor(4.7)); // 4
console.log('値: 4.3');
console.log(' Math.round:', Math.round(4.3)); // 4
console.log(' Math.floor:', Math.floor(4.3)); // 4

四捨五入では4.75になりますが、切り捨てでは4になります。 年齢計算などでは通常、切り捨てを使用します。

実践的な使い分け例

商品の箱数計算で使い分けを見てみましょう。

// 商品の箱数計算
function calculateBoxes(items, itemsPerBox) {
const exactBoxes = items / itemsPerBox;
console.log(`商品数: ${items}個, 1箱あたり: ${itemsPerBox}`);
console.log(`正確な箱数: ${exactBoxes}`);
console.log(`四捨五入: ${Math.round(exactBoxes)}`);
console.log(`切り上げ: ${Math.ceil(exactBoxes)}`);
return {
exact: exactBoxes,
rounded: Math.round(exactBoxes),
ceiled: Math.ceil(exactBoxes)
};
}
calculateBoxes(23, 10); // 2.3箱 → 四捨五入2箱, 切り上げ3箱
calculateBoxes(27, 10); // 2.7箱 → 四捨五入3箱, 切り上げ3箱

この例では、用途に応じて適切なメソッドを選択できます。 実際の業務では、箱数は切り上げで計算することが多いですね。

小数点の桁数を指定した四捨五入をマスターしよう

任意の桁数で四捨五入する方法

Math.roundは小数点第1位での四捨五入しかできません。 任意の桁数で四捨五入するには、工夫が必要です。

// 指定した桁数で四捨五入する関数
function roundToDecimal(value, decimals) {
const factor = Math.pow(10, decimals);
return Math.round(value * factor) / factor;
}
console.log('=== 小数点第2位で四捨五入 ===');
console.log(roundToDecimal(3.14159, 2)); // 3.14
console.log(roundToDecimal(3.14659, 2)); // 3.15
console.log(roundToDecimal(2.671, 2)); // 2.67

この関数では、指定した桁数分だけ10倍して四捨五入し、元に戻しています。 3.14159を小数点第2位で四捨五入すると3.14になりますね。

より安全な四捨五入関数

実用的な場面では、エラーハンドリングも重要です。

// より堅牢な実装
function safeRoundToDecimal(value, decimals = 0) {
// 入力値の検証
if (typeof value !== 'number' || isNaN(value)) {
throw new Error('有効な数値を入力してください');
}
if (typeof decimals !== 'number' || decimals < 0 || !Number.isInteger(decimals)) {
throw new Error('小数点以下の桁数は0以上の整数である必要があります');
}
// 浮動小数点数の精度問題を避けるため、Number.parseFloatを使用
const factor = Math.pow(10, decimals);
const result = Math.round(value * factor) / factor;
return Number.parseFloat(result.toFixed(decimals));
}
console.log('=== 安全な四捨五入関数のテスト ===');
console.log(safeRoundToDecimal(3.14159, 2)); // 3.14
console.log(safeRoundToDecimal(1.005, 2)); // 1.01(浮動小数点数問題対応)

この実装では、入力値をしっかりと検証しています。 エラーハンドリングにより、予期しない動作を防げますね。

実践的な活用例で理解を深めよう

通貨計算での四捨五入

実際の開発でよく使われる通貨計算での四捨五入を見てみましょう。

// 通貨計算用の四捨五入クラス
class CurrencyCalculator {
constructor(currencyCode = 'JPY', decimals = 0) {
this.currencyCode = currencyCode;
this.decimals = decimals;
}
round(amount) {
const factor = Math.pow(10, this.decimals);
return Math.round(amount * factor) / factor;
}
format(amount) {
const rounded = this.round(amount);
return new Intl.NumberFormat('ja-JP', {
style: 'currency',
currency: this.currencyCode,
minimumFractionDigits: this.decimals,
maximumFractionDigits: this.decimals
}).format(rounded);
}
calculateTax(price, taxRate = 0.1) {
const tax = this.round(price * taxRate);
const total = this.round(price + tax);
return {
price: this.round(price),
tax: tax,
total: total,
formatted: {
price: this.format(price),
tax: this.format(tax),
total: this.format(total)
}
};
}
}
// 日本円計算(小数点なし)
const jpyCalculator = new CurrencyCalculator('JPY', 0);
console.log('=== 消費税計算(日本円) ===');
const taxResult = jpyCalculator.calculateTax(1980);
console.log('商品価格:', taxResult.formatted.price);
console.log('消費税:', taxResult.formatted.tax);
console.log('合計:', taxResult.formatted.total);

このクラスでは、通貨に応じた適切な四捨五入処理を行っています。 日本円では小数点なし、米ドルでは小数点第2位まで処理できますね。

パーセンテージ計算での応用

成績やアンケート結果など、パーセンテージ計算でも四捨五入が重要です。

// パーセンテージ計算クラス
class PercentageCalculator {
static calculate(numerator, denominator, decimals = 1) {
if (denominator === 0) {
throw new Error('分母が0です');
}
const percentage = (numerator / denominator) * 100;
const factor = Math.pow(10, decimals);
return Math.round(percentage * factor) / factor;
}
static formatPercentage(value, decimals = 1) {
return `${value.toFixed(decimals)}%`;
}
}
// 成績計算の例
function calculateGrade(correctAnswers, totalQuestions) {
const percentage = PercentageCalculator.calculate(correctAnswers, totalQuestions, 1);
let grade;
if (percentage >= 90) grade = 'A';
else if (percentage >= 80) grade = 'B';
else if (percentage >= 70) grade = 'C';
else if (percentage >= 60) grade = 'D';
else grade = 'F';
return {
correctAnswers: correctAnswers,
totalQuestions: totalQuestions,
percentage: percentage,
formatted: PercentageCalculator.formatPercentage(percentage, 1),
grade: grade
};
}
console.log('=== 成績計算 ===');
console.log(calculateGrade(47, 50)); // 94.0% → A
console.log(calculateGrade(38, 50)); // 76.0% → C

この実装により、正確な成績計算とグレード判定ができます。 小数点第1位で四捨五入することで、見やすい結果になりますね。

データ可視化での数値処理

グラフやチャートでデータを表示する際の数値処理も重要です。

// データ可視化用の数値処理クラス
class DataVisualizationHelper {
static formatForChart(data, decimals = 1) {
return data.map(value => {
if (typeof value !== 'number' || isNaN(value)) {
return 0;
}
const factor = Math.pow(10, decimals);
return Math.round(value * factor) / factor;
});
}
static normalizeData(data, maxValue = 100) {
const dataMax = Math.max(...data);
const dataMin = Math.min(...data);
const range = dataMax - dataMin;
if (range === 0) {
return data.map(() => maxValue / 2);
}
return data.map(value => {
const normalized = ((value - dataMin) / range) * maxValue;
return Math.round(normalized * 10) / 10; // 小数点第1位で四捨五入
});
}
}
// サンプルデータ
const salesData = [120.5, 134.7, 98.3, 156.9, 187.2];
console.log('=== 元データ ===');
console.log(salesData);
console.log('=== チャート用フォーマット(小数点第1位) ===');
console.log(DataVisualizationHelper.formatForChart(salesData, 1));
console.log('=== 正規化データ(0-100の範囲) ===');
console.log(DataVisualizationHelper.normalizeData(salesData, 100));

この処理により、グラフに表示するデータを適切にフォーマットできます。 正規化により、異なるスケールのデータも比較しやすくなりますね。

よくある間違いと回避方法を身につけよう

浮動小数点数の精度問題

JavaScriptの浮動小数点数計算では、予期しない結果が出ることがあります。

console.log('=== 浮動小数点数の問題例 ===');
// 問題のある計算例
console.log(0.1 + 0.2); // 0.30000000000000004(期待値: 0.3)
console.log(Math.round(0.1 + 0.2)); // 0(期待値: 1 if rounding 0.3)
// 正しい対処法
function safeMath(operation) {
// 計算結果を適切な桁数で四捨五入
return Math.round(operation * 1000000000) / 1000000000;
}
console.log('=== 修正された計算 ===');
console.log(safeMath(0.1 + 0.2)); // 0.3

この問題は、JavaScriptの数値が浮動小数点数で表現されるために起こります。 適切な桁数で四捨五入することで、期待した結果が得られますね。

負数の四捨五入の注意点

負数の四捨五入では、直感と異なる結果になる場合があります。

console.log('=== 負数の四捨五入 ===');
// 期待される動作と実際の動作
const negativeNumbers = [-1.5, -2.5, -3.5, -4.5];
negativeNumbers.forEach(num => {
console.log(`Math.round(${num}) = ${Math.round(num)}`);
});
// 「0に近い方向に丸める」バージョン
function roundTowardsZero(value) {
return value >= 0 ? Math.floor(value + 0.5) : Math.ceil(value - 0.5);
}
console.log('=== 異なる四捨五入方法の比較 ===');
const testValues = [-2.5, -1.5, 1.5, 2.5];
testValues.forEach(value => {
console.log(`値: ${value}`);
console.log(` Math.round: ${Math.round(value)}`);
console.log(` 0方向: ${roundTowardsZero(value)}`);
});

負数の四捨五入では、Math.roundは0に近い方向に丸めます。 用途に応じて、適切な四捨五入方法を選択することが重要ですね。

型変換での予期しない動作

Math.roundに渡す値の型変換で問題が起こる場合があります。

console.log('=== 型変換の問題 ===');
// 文字列の自動変換
console.log(Math.round('4.7')); // 5(正常)
console.log(Math.round('4.7abc')); // NaN(問題)
console.log(Math.round('')); // 0(予期しない?)
// オブジェクトの変換
console.log(Math.round(null)); // 0
console.log(Math.round(undefined)); // NaN
console.log(Math.round([])); // 0(空配列)
// 安全な四捨五入関数
function safeRound(value, decimals = 0) {
// 厳密な型チェック
if (value === null || value === undefined) {
throw new Error('値がnullまたはundefinedです');
}
// 数値への変換を試行
const numValue = Number(value);
if (isNaN(numValue)) {
throw new Error(`"${value}"は有効な数値ではありません`);
}
if (!isFinite(numValue)) {
throw new Error('無限大の値は処理できません');
}
const factor = Math.pow(10, decimals);
return Math.round(numValue * factor) / factor;
}
console.log('=== 安全な四捨五入のテスト ===');
const testInputs = [4.7, '4.7', '4.7abc', null, undefined];
testInputs.forEach(input => {
try {
const result = safeRound(input, 1);
console.log(`safeRound(${JSON.stringify(input)}) = ${result}`);
} catch (error) {
console.log(`safeRound(${JSON.stringify(input)}) = エラー: ${error.message}`);
}
});

この実装では、入力値を厳密にチェックしています。 予期しない値に対してエラーを投げることで、バグを早期発見できますね。

まとめ:Math.roundで正確な数値処理を実現しよう

JavaScriptのMath.roundを使った四捨五入について、基礎から実践まで詳しく解説しました。

Math.roundの重要なポイントをおさらいしましょう。

  • 基本動作:小数点第1位で四捨五入(0.5以上で切り上げ)
  • 他メソッドとの違い:ceil(切り上げ)、floor(切り捨て)との使い分け
  • 任意桁数:掛け算と割り算を組み合わせて任意の桁数で四捨五入
  • 実用性:通貨計算、パーセンテージ、データ可視化での活用

注意すべきポイントも忘れずに。

  • 浮動小数点数の精度問題
  • 負数の四捨五入動作
  • 型変換での予期しない結果
  • 適切なエラーハンドリングの実装

Math.roundをマスターすることで、より正確で実用的な数値処理が可能になります。 実際のプロジェクトでは、用途に応じて適切な精度と安全性を考慮した実装を心がけてください。

まずは基本的な使い方から始めて、徐々に高度なテクニックを活用してみませんか? きっと「数値処理がこんなに簡単だったんだ!」と感じられるはずです。

関連記事