Converti un numero intero in un numero float


127

Come faccio a convertire un valore intero in float64 tipo?

Provai

float(integer_value)

Ma questo non funziona. E non riesco a trovare alcun pacchetto che lo faccia su Golang.org

Come posso ottenere float64valori da valori interi?

Risposte:


180

Non esiste un floattipo. Sembra che tu voglia float64. È inoltre possibile utilizzare float32se è necessario solo un valore in virgola mobile a precisione singola.

package main

import "fmt"

func main() {
    i := 5
    f := float64(i)
    fmt.Printf("f is %f\n", f)
}

Notiamo che il compilatore di go non è intuitivo al riguardo. a := uint(8); b := uint(5); c := float32(a/b)sarà di c1.000.000, non di 1.6.
isomorfismi

33

Solo per completezza, ecco un link alla documentazione di Golang che descrive tutti i tipi . Nel tuo caso si tratta di tipi numerici:

uint8       the set of all unsigned  8-bit integers (0 to 255)
uint16      the set of all unsigned 16-bit integers (0 to 65535)
uint32      the set of all unsigned 32-bit integers (0 to 4294967295)
uint64      the set of all unsigned 64-bit integers (0 to 18446744073709551615)

int8        the set of all signed  8-bit integers (-128 to 127)
int16       the set of all signed 16-bit integers (-32768 to 32767)
int32       the set of all signed 32-bit integers (-2147483648 to 2147483647)
int64       the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)

float32     the set of all IEEE-754 32-bit floating-point numbers
float64     the set of all IEEE-754 64-bit floating-point numbers

complex64   the set of all complex numbers with float32 real and imaginary parts
complex128  the set of all complex numbers with float64 real and imaginary parts

byte        alias for uint8
rune        alias for int32

Ciò significa che è necessario utilizzare float64(integer_value).


2

intutils.ToFloat32

// ToFloat32 converts a int num to a float32 num
func ToFloat32(in int) float32 {
    return float32(in)
}

// ToFloat64 converts a int num to a float64 num
func ToFloat64(in int) float64 {
    return float64(in)
}

0

Le conversioni di tipo T () dove T è il tipo di dati desiderato del risultato sono abbastanza semplici in GoLang.

Nel mio programma, eseguo la scansione di un intero i dall'input dell'utente, eseguo una conversione del tipo su di esso e lo memorizzo nella variabile f . L'output stampa l' float64equivalente intdell'input. float32il tipo di dati è disponibile anche in GoLang

Codice:

package main
import "fmt"
func main() {
    var i int
    fmt.Println("Enter an Integer input: ")
    fmt.Scanf("%d", &i)
    f := float64(i)
    fmt.Printf("The float64 representation of %d is %f\n", i, f)
}

Soluzione:

>>> Enter an Integer input:
>>> 232332
>>> The float64 representation of 232332 is 232332.000000
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.