循環小数 2/ 7について
- 2 / 7 は、 0.28571428571...
ですので、
- 1桁目 → 2
- 2桁目 → 8
- 3桁目 → 5
- 4桁目 → 7
- 5桁目 → 1
- 6桁目 → 4
となります。
今回は、これを一つずつ算出しようと思います。
const decimal = 2 / 7
まず、2/7を定義
const multiplied = decimal * Math.pow(10, i)
これをi桁目分べき乗します
const integer = Math.floor(multiplied)
小数点以下を切り下げます。
integer = String(integer);
stringにします。(次の動作をするため)
const lastDigit = integer.slice(-1)
文字列の下一桁目を出力します。
var exportNum = Number(lastDigit);
求値です。
全体のコード
for (let i = 1; i < 7; i++) {
const decimal = 2 / 7
const multiplied = decimal * Math.pow(10, i)
const integer = Math.floor(multiplied)
integer = String(integer)
const lastDigit = string.slice(-1)
const exportNum = Number(lastDigit)
alert(`小数点${i}桁目 : ${ exportNum }`)
}
最後に
最後まで見ていただき、ありがとうございました。