LoginSignup
1
0

More than 5 years have passed since last update.

循環小数 2/7の1 ~ 6桁目までを算出する

Last updated at Posted at 2019-01-03

循環小数 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 }`)
}

最後に

最後まで見ていただき、ありがとうございました。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0