如何在golang中读取浮点数表示法?

huangapple go评论78阅读模式
英文:

How to read float notation in golang?

问题

当从结构体映射中打印出一些值时,我看到某些float64值使用了替代表示法。测试通过了,但是你如何读取这种表示法(4e-06)?这个值确实等同于"0.000004"吗?

package main

import (
	"fmt"
	"strconv"
	"testing"
)

func TestXxx(t *testing.T) {

	num := fmt.Sprintf("%f", float64(1.225788)-float64(1.225784)) // 0.000004  
	f, _ := strconv.ParseFloat(num, 64)
	if f == 0.000004 {
		t.Log("Success")
	} else {
		t.Error("Not Equal", num)
	}

	if getFloat(f) == 0.000004 {
		t.Log("Success")
	}else{
		t.Error("Fail", getFloat(f))
	}
}

func getFloat(f float64) float64 {
	fmt.Println("My Float:",f) //  4e-06
	return f
}
英文:

When printing out some values from a map of structs. I see certain float64 values with alternative notation. The test passes but how do you read this notation (4e-06). Is this value indeed the same as "0.000004"?

package main

import (
	"fmt"
	"strconv"
	"testing"
)

func TestXxx(t *testing.T) {

	num := fmt.Sprintf("%f", float64(1.225788)-float64(1.225784)) // 0.000004  
	f, _ := strconv.ParseFloat(num, 64)
	if f == 0.000004 {
		t.Log("Success")
	} else {
		t.Error("Not Equal", num)
	}

	if getFloat(f) == 0.000004 {
		t.Log("Success")
	}else{
		t.Error("Fail", getFloat(f))
	}
}

func getFloat(f float64) float64 {
	fmt.Println("My Float:",f) //  4e-06
	return f
}

答案1

得分: 8

这个符号被称为科学计数法,它是一种以紧凑、简短的形式打印非常小或非常大的数字的便捷方式。

它的形式为

m × 10n

(m 乘以 10 的 n 次方)

在编程语言中,它被写作/打印为:

men

参见规范:浮点数字面量

你的数字是:4e-06,其中 m=4n=-6,意味着 4*10-6 等于 0.000004

英文:

The notation is called Scientific notation, and it is a convenient way to print very small or very large numbers in compact, short form.

It has the form of

> m × 10<sup>n</sup>

(m times ten raised to the power of n)

In programming languages it is written / printed as:

> men

See Spec: Floating-point literals.

Your number: 4e-06, where m=4 and n=-6, which means 4*10<sup>-6</sup> which equals to 0.000004.

答案2

得分: 3

为了以规范的方式打印浮点数,你可以像下面的示例一样操作:

package main

import (
    "fmt"
    "strconv"
)


func main() {
    a, _ := strconv.ParseFloat("0.000004", 64)
    b, _ := strconv.ParseFloat("0.0000000004", 64)
    c := fmt.Sprintf("10.0004")
    cc, _ := strconv.ParseFloat(c, 64)
    fmt.Printf("%.6f\n", a)  // 小数点后6位
    fmt.Printf("%.10f\n", b) // 小数点后10位
    fmt.Printf("%.4f\n", cc) // 小数点后4位
}

输出结果:

0.000004
0.0000000004
10.0004
英文:

In order to print your floats in a regular way you can do something like this example:

package main

import (
    &quot;fmt&quot;
    &quot;strconv&quot;
)


func main() {
	a, _ := strconv.ParseFloat(&quot;0.000004&quot;, 64)
	b, _ := strconv.ParseFloat(&quot;0.0000000004&quot;, 64)
	c := fmt.Sprintf(&quot;10.0004&quot;)
	cc, _ := strconv.ParseFloat(c, 64)
	fmt.Printf(&quot;%.6f\n&quot;, a) 	// 6 numbers after the point
	fmt.Printf(&quot;%.10f\n&quot;, b)	// 10 numbers afer the point
	fmt.Printf(&quot;%.4f\n&quot;, cc)	// 4 numbers after the point
}

Output:

0.000004
0.0000000004
10.0004

答案3

得分: 2

这是相同的数字。如果你不喜欢科学计数法,你可以使用fmt.Printf("My Float: %.6f\n", f)。(这种格式要求在小数点后打印6位数字。)

英文:

It is the same number. You can use fmt.Printf(&quot;My Float: %.6f\n&quot;,f) if you don't like the scientific notation. (This format requests that 6 digits will be printed after the decimal point.)

huangapple
  • 本文由 发表于 2016年12月19日 04:16:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/41212445.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定