Solidity

獲取數字的第一位

  • August 25, 2022

在 Solidity 中,如何獲取兩位數的第一個數字,65例如?

   function firstdigit(uint256 number) public pure returns (uint256) {
       uint256 num = number;
       uint256 first = num / 10;
       return first;
   }

看看這個解決方案。我展示瞭如何在數字的任何索引處獲取任何數字,以及如何獲取數字的第一個數字(從左到右)。如何獲取數字的最後一位數字。閱讀逐步解釋它的作用的評論。

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.16;

// Author: Jeremy Then

contract Digit {
   
   // Getting any digit of a number `n` by calculating 10 to the power of `index`
   // to divide the number by that result to remove the digits to the right of the
   // digit that we want. And then using % 10 to get the last digit (from left to right) 
   // (that should be our desired number since we removed the digits to the right of it.
   function getDigitAtIndex(uint256 n, uint index) public pure returns(uint256) {

       // If n is 123456 and we want the digit at index 2, it means we want the number `4`
       // counting from right to left.
       // If we do 10 ** 2 = its 100.
       // 123456 / 100 = 1234
       // now we do 1234 % 10 = 4, our desired digit.

       return (n / (10 ** index)) % 10;
   }

   // To get the first digit, we need to know how many digit the number has
   // to be able to create a big base 10 number - 1 to then divide our number by the result
   // and get our answer. It's similar to the getDigitAtIndex() function,
   // but since we want the first digit (from left to right) then we just
   // to divide it by the bit number so we get rid of all the numbers to the right of it.
   function getFirstDigit(uint256 n) public pure returns(uint256) {

       // If n = 654321, then we see it has 6 digits, which log10(123456) = 6
       // 10 ** (6 - 1) = 100000
       // 654321 / 100000 = 6

       uint256 countOfDigits = log10(n);
       return n / (10 ** (countOfDigits - 1));
   }

   // Getting the last digit is easy. Just 'divide' by 10 and get the remainder (using the modulo % operator)
   function getLastDigit(uint256 n) public pure returns(uint256) {
       return n % 10;
   }

   // Inefficient log10 implementation, just to show how to get the first digit of a number
   // This function is used to know how many digits a number has
   function log10(uint256 n) public pure returns(uint256) {
       uint256 count;
       while(n != 0) {
           count++;
           n /= 10;
       }
       return count;
   }

}

Solidity 沒有實現log10數學函式,所以我創建了一個簡單的,它可能效率不高,但對於這個簡單的案例來說沒問題。

您可以用任何語言嘗試這些簡單的公式,它們的工作原理應該相同。

引用自:https://ethereum.stackexchange.com/questions/134302