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
+7.7k Golang : How to protect your source code from client, hosting company or hacker?
+6k Golang : Variadic function arguments sanity check example
+11.3k Golang : Google Drive API upload and rename example
+10.2k Golang : How to check if a string starts or ends with certain characters or words?
+9k Golang : Fuzzy string search or approximate string matching example
+8.8k Golang : Get local time and equivalent time in different time zone
+6.5k Golang : HTTP Server Example
+14.3k Golang : Merge video(OpenCV) and audio(PortAudio) into a mp4 file
+8k Golang : Select region of interest with mouse click and crop from image
+4.3k Fix ERROR 2003 (HY000): Can't connect to MySQL server on 'IP address' (111)
+10.3k Golang : How to check if your program is running in a terminal
+13k Golang : Read large file with bufio.Scanner cause token too long error