<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                [TOC] >[success] # 數字的處理 ~~~ 1.python 中要格外的注意浮點類型的數字進行,相對的 ,因為python底層cpu的浮點運算單元,保存的時候是按照原始表現 形式,例如: a=2.1 b=4.2 print(a+b) # 6.300000000000001 正是因為這種原因,所引入下面處理方法 ~~~ >[danger] ##### 取整操作 --- round ~~~ round(number, ndigits=None) 1.round 中有兩個參數,第一個是要處理的值,第二個參數,為正整數時是要保留的小數位,負數是要舍棄的整數數位,從個 位開始 2.round 不像四舍五入,逢五進一,而是默認最近的偶數,例如: "2.4 輸出 2 ,2.5輸出的2,3.5輸出的是4,2.6 輸出3,3.6輸出的是4" ~~~ ~~~ num1 = round(1.23,1) num2 = round(1.25,1) num3 = round(1.35,1) num4 = round(1.35) num5 = round(105,-1) print(num1, num2, num3, num4,num5) 打印結果: 1.2 1.2 1.4 1 100 ~~~ >[danger] ##### 將數字以固定格式輸出展示 --- format ~~~ 1.如果只是將,所得到的數字做展示處理,可以使用format ~~~ ~~~ x=1.233 a = format(x,'0.2f') print(a,type(a)) 打印結果: 1.23 <class 'str'> ~~~ >[danger] ##### 更精準的操作 --- decimal ~~~ 1.當我們在金融等,對數字要求敏感的代碼的時候,我們可以為此犧牲掉一些性能,使用decimal 2.當使用decima 對象進行轉化求和的時候,對比也需要使用decimal 對象來進行比較 ~~~ ~~~ from decimal import Decimal a = Decimal('4.2') b = Decimal('2.1') c = a+b print(c) print(a+b == 6.3) 打印結果: 6.3 False ~~~ * 對 decimal 進行四舍五入 ~~~ from decimal import Decimal,localcontext a = Decimal('1.3') b = Decimal('1.7') c = a/b print(c) with localcontext() as ctx: ctx.prec = 3 print(a/b) 打印結果: 0.7647058823529411764705882353 0.765 ~~~ >[danger] ##### 對二進制/八進制/十進制的轉化 * 將十進制 分別轉換成 二進制/八進制/十六進制 ~~~ x = 1234 b = bin(x) o = oct(x) h = hex(x) print(b, o, h,) 打印結果: 0b10011010010 # 二進制 0o2322 # 八進制 0x4d2 # 十六進制 ~~~ * 去掉前綴,將十進制轉換成二進制/八進制/十六進制 ~~~ x = 1234 b1 = format(x, 'b') o1 = format(x, 'o') h1 = format(x, 'x') print(b1, o1, h1) 打印結果: 10011010010 2322 4d2 ~~~ * 將 二進制/八進制/十進制轉換成 十進制 ~~~ b1 = int('10011010010', 2) o1 = int('2322', 8) h1 = int('4d2', 16) print(b1, o1, h1) 打印結果: 1234 1234 1234 ~~~ >[danger] ##### 分數格式展示 --- fractions * 分數展示 ~~~ from fractions import Fraction a = Fraction(5, 4) b = Fraction(7, 16) print(a,b) 打印結果: 5/4 7/16 ~~~ * 獲得分子分母 ~~~ c = a*b print(c) print(c.numerator) print(c.denominator) print(float(c)) 打印結果: 35/64 35 64 0.546875 ~~~ * 小數轉換掙分數 ~~~ x = 3.75 print(Fraction(*x.as_integer_ratio())) 打印結果: 15/4 ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看