小编iko*_*ool的帖子

如何将 float64 数字截断为特定精度?

我想截断1.234567为3位小数浮点数,但结果不是我想要的。

例如:1.234567=>1.234

package main

import (
    "strconv"
    "fmt"
)

func main() {
    f := 1.234567
    fmt.Println(strconv.FormatFloat(f, 'f', 3, 64)) //1.235
    fmt.Printf("%.3f", f) //1.235
}
Run Code Online (Sandbox Code Playgroud)

谁能告诉我如何在 Go 中做到这一点?

floating-point truncate go

7
推荐指数
1
解决办法
8935
查看次数

为什么Python中不同的类实例有相同的id?

我尝试在多线程中获取一个类的不同实例,但是 id(instance) 随机重新调整了相同的 id,即使我添加睡眠时间,这种情况仍然会发生,为什么?

#!/usr/bin/python
# coding=utf-8

import random
import threading

class Foo():
    def __init__(self):
        self.num = random.randrange(10000)

    def __str__(self):
        return "rand num is {}".format(self.num)

def getInatance():
    if lock.acquire():
        f = Foo()
        print(id(f), f)
        lock.release()

lock = threading.Lock()
if __name__ == "__main__":
    for x in range(10):
        th = threading.Thread(target=getInatance)
        th.start()

    for th in threading.enumerate():
        if th is not threading.current_thread():
            th.join()

# 2384808866048 rand num is 357
# 2384808640128 rand num is 7143
# 2384808640128 rand num is 900
# 2384808640128 …
Run Code Online (Sandbox Code Playgroud)

python multithreading

0
推荐指数
1
解决办法
62
查看次数

标签 统计

floating-point ×1

go ×1

multithreading ×1

python ×1

truncate ×1