我想截断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 中做到这一点?
我尝试在多线程中获取一个类的不同实例,但是 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)