【问题标题】:GoLang nextafter & %g?GoLang nextafter & %g?
【发布时间】:2014-11-30 18:52:39
【问题描述】:

我只是在浏览 GoLang 的教程,因为我很想学习一门更通用的语言(只知道 javascript)。 Tut

我不明白 %g 和 math.Nextafter 是什么意思?在查看文档后,它几乎没有帮助。

问题: 谁能给我一个更好的关于%g语法和nextafter()方法的概述;也许是一个可消化的用例?以下是文档链接:fmt // nextAfter

package main

import (
    "fmt"
    "math"
)

func main() {
    fmt.Printf("Now you have %G problems.",
        math.Nextafter(2, 3))
}

【问题讨论】:

    标签: go


    【解决方案1】:

    关于 %g 语法,见fmt syntax:

    %g  whichever of %e or %f produces more compact output
    %e  scientific notation, e.g. -1234.456e+78
    %f  decimal point but no exponent, e.g. 123.456
    

    NextAfter() 返回的float64 将使用科学计数法或小数点表示,具体取决于哪个更紧凑。

    NextAfter的返回值见“Why does math.Nextafter(2,3) in Go increment by 0.0000000000000004 instead of 0.0000000000000001?”

    您会在“Go by Example: String Formatting”中看到大量的 Go 字符串格式:一个参数用于格式,一个参数用于要格式化的值。
    这些格式(“动词”)在`pkg/fmt/ 中列出。

    【讨论】:

    • 为什么printf函数要带两个参数?
    • @MatthewHarwood:阅读 printf 的文档。
    • 是的,我会的。有很多事情超出了我的理解范围。例如类型 float64。我会阅读更多内容。
    • @MatthewHarwood float64 是一个 64 位浮点数。见en.wikipedia.org/wiki/Double-precision_floating-point_format
    猜你喜欢
    • 2016-08-07
    • 1970-01-01
    • 2012-08-17
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-20
    相关资源
    最近更新 更多