number Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/number/ Wed, 07 Jul 2021 13:00:12 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.1 https://i0.wp.com/golangbyexamples.com/wp-content/uploads/2021/05/cropped-go_border-1.png?fit=32%2C32&ssl=1 number Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/number/ 32 32 159787465 Golang Regex: Match a floating-point number in Regular Expression https://vikasboss.github.io/golang-regex-floating-point-number/ https://vikasboss.github.io/golang-regex-floating-point-number/#respond Wed, 07 Jul 2021 12:56:41 +0000 https://vikasboss.github.io/?p=5864 Overview A floating-point number could have below properties It could have a negative and positive sign The integer part could be optional when the decimal part is present The dot and decimal...

The post Golang Regex: Match a floating-point number in Regular Expression appeared first on Welcome To Golang By Example.

]]>
Overview

A floating-point number could have below properties

  • It could have a negative and positive sign
  • The integer part could be optional when the decimal part is present
  • The dot and decimal part could be optional if the integer part is present
  • It could have an exponent or not

So below are valid floating-point numbers

1.2
.12
12
12.
+1.2
-1.2
1.2e3

Below are invalid floating points 

  • An empty string
  • + or – sign only
  • A single dot
  • A prefix of multiple 0. For eg 00.1 or 001
  • Anything like +. or –
  • A dot just before exponent.  Eg 1.e2
  • Any other char before or after the floating-point number. Eg a1.3 or a1.3b or 1.3b

Below are examples of invalid floats

""
.
00.1
001
+
-
+.
-.
1.e2
a1.2
1.2b
a1.2b

Let’s first see a simple regex which only matches only the integer, dot, and decimal parts.

^(?:(?:0|[1-9]\d*)(?:\.\d*)?|\.\d+)$

On a high level, the entire regex has two parts which are in OR relation

  • (?:0|[1-9]\d*)(?:\.\d*)? – This captures the part where the integer part is always present and the decimal part is optional
  • \.\d+ – This captures the part where the integer part is not present and the decimal part is always present.

Let’s dissect this regex

Let’s make it more complex by having it accept a negative or a positive sign. Note that negative or positive sign is optional

^[+\-]?(?:(?:0|[1-9]\d*)(?:\.\d*)?|\.\d+)$

The regex is the same as the earlier regex. We just added the optional positive negative sign in front

  • [+\-] – Match either positive or negative sign.
  • ? – Matching either positive or negative sign is optional

Let’s also add an exponent part to the regex. Again note that the exponent part is optional. This regex is the same as the previous regex. We just added the exponent part at the end

^[+\-]?(?:(?:0|[1-9]\d*)(?:\.\d*)?|\.\d+)(?:\d[eE][+\-]?\d+)?$

Let’s dissect the exponent part

  • (?: – It means non-capturing group
  • \d – Match one digit. This is to prevent numbers like 1.e2
  • [eE] – Match either lowercase e or uppercase E
  • [+\-] – Match either positive or negative sign. The matching either positive or negative sign is optional
  • \d+ – Match zero or more digits
  • )? – Entire regex  expression is optional

Program

Now see an example of this regular expression in action

package main

import (
	"fmt"
	"regexp"
)

func main() {
	sampleRegex := regexp.MustCompile(`^[+\-]?(?:(?:0|[1-9]\d*)(?:\.\d*)?|\.\d+)(?:\d[eE][+\-]?\d+)?$`)

	fmt.Println("Valid Inputs")
	match := sampleRegex.MatchString("1.2")
	fmt.Printf("For 1.2: %t\n", match)

	match = sampleRegex.MatchString(".12")
	fmt.Printf("For .12: %t\n", match)

	match = sampleRegex.MatchString("12")
	fmt.Printf("For 12: %t\n", match)

	match = sampleRegex.MatchString("12.")
	fmt.Printf("For 12.: %t\n", match)

	match = sampleRegex.MatchString("+1.2")
	fmt.Printf("For +1.2.: %t\n", match)

	match = sampleRegex.MatchString("-1.2")
	fmt.Printf("For -1.2.: %t\n", match)

	match = sampleRegex.MatchString("1.2e3")
	fmt.Printf("For 1.2e3.: %t\n", match)

	fmt.Println("\nInValid Inputs")
	match = sampleRegex.MatchString(".")
	fmt.Printf("For .: %t\n", match)

	match = sampleRegex.MatchString("")
	fmt.Printf("For empty string: %t\n", match)

	match = sampleRegex.MatchString("00.1")
	fmt.Printf("For 00.1: %t\n", match)

	match = sampleRegex.MatchString("001")
	fmt.Printf("For 001 %t\n", match)

	match = sampleRegex.MatchString("+")
	fmt.Printf("For +: %t\n", match)

	match = sampleRegex.MatchString("-")
	fmt.Printf("For -: %t\n", match)

	match = sampleRegex.MatchString("+.")
	fmt.Printf("For +.: %t\n", match)

	match = sampleRegex.MatchString("-.")
	fmt.Printf("For -.: %t\n", match)

	match = sampleRegex.MatchString("1.e2")
	fmt.Printf("For 1.e2: %t\n", match)

	match = sampleRegex.MatchString(".e2")
	fmt.Printf("For .e2: %t\n", match)

	match = sampleRegex.MatchString("a1.2")
	fmt.Printf("For a1.2 %t\n", match)

	match = sampleRegex.MatchString("1.2b")
	fmt.Printf("For 1.2b %t\n", match)

	match = sampleRegex.MatchString("a1.2b")
	fmt.Printf("For a1.2b %t\n", match)
}

Output

Valid Inputs
For 1.2: true
For .12: true
For 12: true
For 12.: true
For +1.2.: true
For -1.2.: true
For 1.2e3.: true

InValid Inputs
For .: false
For empty string: false
For 00.1: false
For 001 false
For +: false
For -: false
For +.: false
For -.: false
For 1.e2: false
For .e2: false
For a1.2 false
For 1.2b false
For a1.2b false

For all the valid inputs discussed above the program prints true

Valid Inputs
For 1.2: true
For .12: true
For 12: true
For 12.: true
For +1.2.: true
For -1.2.: true
For 1.2e3.: true

And for all the invalid inputs discussed above it gives false

InValid Inputs
For .: false
For empty string: false
For 00.1: false
For 001 false
For +: false
For -: false
For +.: false
For -.: false
For 1.e2: false
For .e2: false
For a1.2 false
For 1.2b false
For a1.2b false

Please try it out and post in the comments if in any case, this regex doesn’t work.

The above regex is used to validate if a given string is a number. If you want to find if an input string contains a number as a substring then we need to remove the anchor characters at the start and the end which is removing the caret (^) at the start and the dollar ($) character at the end

So the regex will be

[+\-]?(?:(?:0|[1-9]\d*)(?:\.\d*)?|\.\d+)(?:\d[eE][+\-]?\d+)?

This is all about matching floating point numbers through regex in golang. Hope you have liked this article. Please share feedback in the comments.

Also, check out our Golang advance tutorial Series – Golang Advance Tutorial

The post Golang Regex: Match a floating-point number in Regular Expression appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/golang-regex-floating-point-number/feed/ 0 5864
Get ASCII code/value of any Alphabet or Number in Go (Golang) https://vikasboss.github.io/get-ascii-value-alphabet-go/ https://vikasboss.github.io/get-ascii-value-alphabet-go/#respond Fri, 17 Apr 2020 18:19:20 +0000 https://vikasboss.github.io/?p=2039 range over a string can give the ASCII of all the characters in the string. In below code we are printing the ASCII value of a lowercase alphabets, uppercase alphabets and numbers....

The post Get ASCII code/value of any Alphabet or Number in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
range over a string can give the ASCII of all the characters in the string. In below code we are printing the ASCII value of a lowercase alphabets, uppercase alphabets and numbers.

package main

import "fmt"

func main() {
    lowercase := "abcdefghijklmnopqrstunwxyz"
    for _, c := range lowercase {
        fmt.Println(c)
    }

    uppercase := "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    for _, c := range uppercase {
        fmt.Println(c)
    }

    numbers := "0123456789"
    for _, n := range numbers {
        fmt.Println(n)
    }
}

Output:

97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
110
119
120
121
122
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
48
49
50
51
52
53
54
55
56
57

The post Get ASCII code/value of any Alphabet or Number in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/get-ascii-value-alphabet-go/feed/ 0 2039
Check if a number is negative or positive in Go (Golang) https://vikasboss.github.io/num-positive-negative-go/ https://vikasboss.github.io/num-positive-negative-go/#respond Sat, 28 Mar 2020 10:27:10 +0000 https://vikasboss.github.io/?p=1873 Overview math package of GO provides a Signbit method that can be used to check whether a given number is negative or positive. It returns true for a negative number It returns...

The post Check if a number is negative or positive in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

math package of GO provides a Signbit method that can be used to check whether a given number is negative or positive.

  • It returns true for a negative number
  • It returns false for a positive number

Below is the signature of the function. It takes input a float and returns a bool

func Signbit(x float64) bool 

Code

package main

import (
    "fmt"
    "math"
)

func main() {
    //Will return false for positive
    res := math.Signbit(4)
    fmt.Println(res)

    //Will return true for negative
    res = math.Signbit(-4)
    fmt.Println(res)

    //Will return false for zerp
    res = math.Signbit(0)
    fmt.Println(res)

    //Will return false for positive float
    res = math.Signbit(-0)
    fmt.Println(res)
}

Output:

false
true
false
false

The post Check if a number is negative or positive in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/num-positive-negative-go/feed/ 0 1873
Cube Root of a number in Go (Golang) https://vikasboss.github.io/cube-root-number-golang/ https://vikasboss.github.io/cube-root-number-golang/#respond Thu, 26 Mar 2020 05:32:12 +0000 https://vikasboss.github.io/?p=1842 Overview math package of GO provides a Cbrt method that can be used to get the cube root of that number. Below is the signature of the function. It takes input a...

The post Cube Root of a number in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

math package of GO provides a Cbrt method that can be used to get the cube root of that number.

Below is the signature of the function. It takes input a float and also returns a float.

func Cbrt(x float64) float64

Some special cases of Cbrt function are

  • Cbrt(±0) = ±0
  • Cbrt(±Inf) = ±Inf
  • Cbrt(NaN) = NaN

Code:

package main

import (
    "fmt"
    "math"
)

func main() {
    res := math.Cbrt(8)
    fmt.Println(res)

    res = math.Cbrt(27)
    fmt.Println(res)

    res = math.Cbrt(30.33)
    fmt.Println(res)
}

Output:

2
3
3.118584170228812

The post Cube Root of a number in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/cube-root-number-golang/feed/ 0 1842
Get absolute value of a number in Go (Golang) https://vikasboss.github.io/absolute-value-number-golang/ https://vikasboss.github.io/absolute-value-number-golang/#respond Thu, 26 Mar 2020 05:02:27 +0000 https://vikasboss.github.io/?p=1836 Overview math package of GO provides an Abs method that can be used to get the absolute value of a number. Below is the signature of the function. It takes input a...

The post Get absolute value of a number in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

math package of GO provides an Abs method that can be used to get the absolute value of a number.

Below is the signature of the function. It takes input a float and also returns a float.

func Abs(x float64) float64

Some special cases of Abs function are

  • Abs(±0) = ±0
  • Abs(±Inf) = ±Inf
  • Abs(NaN) = NaN

Code:

package main

import (
    "fmt"
    "math"
)

func main() {
    res := math.Abs(-2)
    fmt.Println(res)

    res = math.Abs(2)
    fmt.Println(res)

    res = math.Abs(3.5)
    fmt.Println(res)

    res = math.Abs(-3.5)
    fmt.Println(res)
}

Output:

2
2
3.5
3.5

The post Get absolute value of a number in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/absolute-value-number-golang/feed/ 0 1836
Floor of a number in Go (Golang) https://vikasboss.github.io/floor-number-golang/ https://vikasboss.github.io/floor-number-golang/#respond Wed, 25 Mar 2020 17:06:40 +0000 https://vikasboss.github.io/?p=1809 Overview math package of go provides a Floor method that can be used to get the ceil of a number. Floor of a number is the greatest integer value less than or...

The post Floor of a number in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

math package of go provides a Floor method that can be used to get the ceil of a number. Floor of a number is the greatest integer value less than or equal to that number.

Below is the signature of the function. It takes input a float and also returns a float.

func Floor(x float64) float64

Some special cases of floor function are

  • Floor(±0) = ±0
  • Floor(±Inf) = ±Inf
  • Floor(NaN) = NaN

Code:

package main

import (
    "fmt"
    "math"
)

func main() {
    res := math.Floor(1.6)
    fmt.Println(res)
  
    res = math.Floor(-1.6)
	fmt.Println(res)
    
    res = math.Floor(1)
    fmt.Println(res)
}

Output:

1
-2
1

The post Floor of a number in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/floor-number-golang/feed/ 0 1809