Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cast from float32 to int in Go

I have tried several ways to cast a float to an int, what I want is to truncate a float so I only get the integer part. I'm using

x := float32(3.1) y,_ := strconv.Atoi((strconv.Ftoa32(x,'f',0))) //y becomes 3 

But if x is 3.9, y will become 4 because this function will round the float32 instead of truncating. Is there a way of truncating instead of rounding? and if so, is it possible to do it without involving strings? (like casting a float to int in C)

like image 891
Goodwine Avatar asked Nov 09 '11 19:11

Goodwine


People also ask

How do you cast to int in go?

In order to convert string to integer type in Golang, you can use the following methods. You can use the strconv package's Atoi() function to convert the string into an integer value. Atoi stands for ASCII to integer. The Atoi() function returns two values: the result of the conversion, and the error (if any).

How do you round a float to an int?

round() method, which converts float to its nearest integer by adding +0.5 to it's value and then truncating it.

Can we convert integer into float?

To convert an integer data type to float you can wrap the integer with float64() or float32. Explanation: Firstly we declare a variable x of type int64 with a value of 5. Then we wrap x with float64(), which converts the integer 5 to float value of 5.00.

How do I convert integers to floats in go?

Converting integers to floats in Go is similar to converting one integer type to another. You can use the built-in type conversions by wrapping float64 () or float32 () around the integer you are converting: This code declares a variable x of type int64 and initializes its value to 57.

How do you cast a type to an integer in go?

The syntax for general type casting is pretty simple. just use that other type name as a function to convert that value. e.g. i := int (32.987) // casting to integer Unlike other languages, Go doesn’t support implicit type conversion.

How to cast int to integer in Golang?

The syntax for general type casting is pretty simple. just use that other type name as a function to convert that value. e.g. i := int (32.987) // casting to integer Unlike other languages, Go doesn’t support implicit type conversion. Although when dividing numbers, implicit conversions happen depending on the scenario.

How to convert float64 type data to INT in C?

You can use int () function to convert float64 type data to an int. Similarly you can use float64 () Example: func check (n int) bool { // count the number of digits var l int = countDigit (n) var dup int = n var sum int = 0 // calculates the sum of digits // raised to power for dup > 0 { **sum += int (math.Pow (float64 (dup % 10), ...


1 Answers

Just use int():

x := float32(3.1) fmt.Println(int(x)) 

Which produces 3 as needed, without having to use string conversions or the like.

like image 176
Chris Bunch Avatar answered Sep 18 '22 20:09

Chris Bunch