Golang : Count number of digits from given integer value
Just a simple program to count number of digits from a given integer value. Need this to calculate if an integer value generated by another function has 1 digit, 2 digits or more. If 1 digit, then the signal is weak, if 2 digits, then the signal is normal and if 3 digits or more .. it means the signal is strong.
Here you go!
package main
import (
"fmt"
)
func CountDigits(i int) (count int) {
for i != 0 {
i /= 10
count = count + 1
}
return count
}
func main() {
var i int
fmt.Println("Enter an integer value : ")
_, err := fmt.Scanf("%d", &i)
if err != nil {
fmt.Println(err)
}
fmt.Println("You have entered a : ", CountDigits(i), "digit(s) integer value")
}
See also : Golang : Number guessing game with user input verification example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+37.9k Golang : How to iterate over a []string(array)
+21.3k Golang : Convert Unix timestamp to UTC timestamp
+50.2k Golang : Check if item is in slice/array
+12.2k Facebook PHP getUser() returns 0
+9.4k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+48.9k Golang : Disable security check for HTTPS(SSL) with bad or expired certificate
+5.1k AWS S3 : Prevent Hotlinking policy
+6.3k Golang : Calculate how many weeks left to go in a given year
+14.6k Golang : Convert date format and separator yyyy-mm-dd to dd-mm-yyyy
+8.7k Golang : Resumable upload to Google Drive(RESTful) example
+6.7k Golang : Test if an input is an Armstrong number example
+6.1k Golang : How to detect if a sentence ends with a punctuation?