久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

將浮點數轉換為一定精度,然后復制為字符串

Convert floating point number to a certain precision, and then copy to string(將浮點數轉換為一定精度,然后復制為字符串)
本文介紹了將浮點數轉換為一定精度,然后復制為字符串的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個浮點數,比如 135.12345678910.我想將該值連接到一個字符串,但只需要 135.123456789.使用打印,我可以通過執行以下操作輕松做到這一點:

I have a floating point number, say 135.12345678910. I want to concatenate that value to a string, but only want 135.123456789. With print, I can easily do this by doing something like:

print "%.9f" % numvar

numvar 是我的原始號碼.有沒有簡單的方法可以做到這一點?

with numvar being my original number. Is there an easy way to do this?

推薦答案

使用 Python <3(例如 2.6 [見評論] 或 2.7),有兩種方法.

With Python < 3 (e.g. 2.6 [see comments] or 2.7), there are two ways to do so.

# Option one
older_method_string = "%.9f" % numvar

# Option two
newer_method_string = "{:.9f}".format(numvar)

但請注意,對于高于 3 的 Python 版本(例如 3.2 或 3.3),選項二是 首選.

But note that for Python versions above 3 (e.g. 3.2 or 3.3), option two is preferred.

有關選項二的更多信息,我建議 this link on string formatting from thePython 文檔.

For more information on option two, I suggest this link on string formatting from the Python documentation.

關于選項一的更多信息,這個鏈接就足夠了,并且有各種標志的信息.

And for more information on option one, this link will suffice and has info on the various flags.

Python 3.6(2016 年 12 月正式發布),添加了 f 字符串字面量,在此處查看更多信息,它擴展了 str.format 方法(使用大括號,例如 f"{numvar:.9f}" 解決了原來的問題),也就是

Python 3.6 (officially released in December of 2016), added the f string literal, see more information here, which extends the str.format method (use of curly braces such that f"{numvar:.9f}" solves the original problem), that is,

# Option 3 (versions 3.6 and higher)
newest_method_string = f"{numvar:.9f}"

解決問題.查看@Or-Duan 的答案以獲取更多信息,但此方法快速.

solves the problem. Check out @Or-Duan's answer for more info, but this method is fast.

這篇關于將浮點數轉換為一定精度,然后復制為字符串的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點小數點/精度)
Converting Float to Dollars and Cents(將浮點數轉換為美元和美分)
What are some possible calculations with numpy or scipy that can return a NaN?(numpy 或 scipy 有哪些可能的計算可以返回 NaN?)
Python float to ratio(Python浮動比率)
How to manage division of huge numbers in Python?(如何在 Python 中管理大量數字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
主站蜘蛛池模板: www.久久久久久久久久久 | 国产美女在线播放 | 精品欧美一区二区精品久久久 | 国产精品一区二区在线 | 亚洲欧美中文日韩在线v日本 | 欧美在线一区视频 | 精品久久国产老人久久综合 | 久久久91 | 中文字幕在线观看第一页 | 亚洲一二三区在线观看 | 国产在线精品一区 | 国产精品乱码一二三区的特点 | 欧美视频1区 | 欧美激情欧美激情在线五月 | 精品在线一区二区三区 | 国产高清视频 | 91麻豆蜜桃一区二区三区 | 欧美黄色网络 | 久久成人精品视频 | 欧美精品二区 | 狠狠影院| 岛国视频 | 国产日韩欧美另类 | 久草视频观看 | 国产欧美日韩久久久 | 欧美成人免费在线视频 | 91福利在线导航 | 亚洲精品久久久一区二区三区 | 国产在线观看 | 亚洲电影专区 | 欧美视频精品 | 国产色片 | 国产精品久久久久久久久久久久久久 | 国产精品美女久久久久aⅴ国产馆 | jizz在线看片| 日韩免费福利视频 | 激情综合五月天 | 91视频网址 | 国产在线www | 精品国产乱码久久久久久1区2区 | 国精产品一区二区三区 |