首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

Python-VBA函数之旅-float函数

  • 25-03-03 04:24
  • 2194
  • 7398
blog.csdn.net

目录

1、float函数:

1-1、Python:

1-2、VBA:

2、相关文章:

个人主页:https://blog.csdn.net/ygb_1024?spm=1010.2135.3001.5421


        float函数在 Python 中的实际应用场景非常广泛,几乎涉及到任何需要处理浮点数数据的场景。常见的应用场景有:

1、数学运算:当你需要执行涉及浮点数的数学运算时,float()函数非常有用。例如,计算平均值、百分比、比率等通常需要浮点数。

2、数据处理与清洗:在数据处理和清洗过程中,经常需要将从文件、数据库或用户输入中读取的字符串形式的数字转换为浮点数。

3、图形和可视化:在创建图形、图表或进行可视化时,float()函数常用于确保坐标、大小或其他数值参数是浮点数类型。

4、货币计算和金融分析:尽管货币计算通常使用decimal模块以确保精度,但在某些情况下,你可能仍然需要将货币值转换为浮点数进行快速计算或与其他系统交互。

5、科学研究与计算:在科学研究领域,尤其是物理、化学、生物学等领域,经常需要进行精确的数学计算,这些计算往往涉及浮点数。

6、机器学习和数据分析:在机器学习和数据分析中,特征值和目标变量通常是浮点数。处理这些数据时,float()函数可以帮助确保数据类型的一致性。

7、与外部库和API交互:许多外部库和API要求输入为特定类型,特别是当处理数字数据时。使用 float()可以确保你的数据符合这些要求。

8、传感器数据和物联网:在处理来自传感器或物联网设备的数据时,这些数据通常以字符串形式提供,并且需要转换为浮点数以进行进一步的分析和处理。

        总之,任何需要将其他数据类型转换为浮点数以便进行数学运算或满足特定函数和库要求的情况,都可以使用float()函数。

1、float函数:
1-1、Python:
  1. # 1.函数:float
  2. # 2.功能:用于将整数和字符串转换为浮点数
  3. # 3.语法:float(x)
  4. # 4.参数:x 整数或字符串
  5. # 5.返回值:返回浮点数;如果参数x未提供,则返回0.0
  6. # 6.说明:
  7. # 6-1、如果float()函数的参数x是一个字符串,则参数x应该是一个十进制数字的字符串表示形式,即参数x必须是能正确转换成浮点型数值的形式,否则会报ValueError错误:
  8. # ValueError: could not convert string to float: '0b1101'
  9. # print(float('0b1101'))
  10. # 7.示例:
  11. # 应用1:数学运算
  12. values = [1, 2, 3, 4, 5]
  13. sum_of_values = sum(values)
  14. count = len(values)
  15. average = float(sum_of_values) / count
  16. print(average)
  17. # 3.0
  18. # 应用2:数据处理与清洗
  19. # 从字符串读取数字并转换为浮点数
  20. data = "3.14, 2.71, 6.02"
  21. numbers = data.split(", ")
  22. floats = [float(num) for num in numbers]
  23. print(floats)
  24. # [3.14, 2.71, 6.02]
  25. # 应用3:图形和可视化
  26. # import matplotlib.pyplot as plt
  27. # # 创建数据点
  28. # x = [1, 2, 3, 4, 5]
  29. # y = ["1.0", "2.5", "3.1", "4.2", "5.0"]
  30. # # 将y转换为浮点数列表
  31. # y_float = [float(yi) for yi in y]
  32. # # 绘制折线图
  33. # plt.plot(x, y_float)
  34. # plt.show()
  35. # 应用4:货币计算和金融分析
  36. # 将货币字符串转换为浮点数
  37. currency_str = "123.45"
  38. currency_float = float(currency_str)
  39. # 进行一些计算,例如计算税率
  40. tax_rate = 0.10
  41. tax = currency_float * tax_rate
  42. print(f"税款是 {tax}")
  43. # 税款是 12.345
  44. # 应用5:科学研究与计算
  45. # 假设我们有一个科学实验中测得的数据集
  46. data_strings = ["1.2345", "2.3456", "3.4567"]
  47. # 将字符串转换为浮点数并计算平均值
  48. data_floats = [float(data) for data in data_strings]
  49. average = sum(data_floats) / len(data_floats)
  50. print(f"数据的平均值为: {average}")
  51. # 数据的平均值为: 2.3455999999999997
  52. # 应用6:机器学习和数据分析
  53. # 假设我们从某个数据源获取了特征和目标变量
  54. features_str = ["0.1", "0.2", "0.3"]
  55. targets_str = ["1.0", "0.0", "1.0"]
  56. # 将字符串转换为浮点数
  57. features_float = [float(feature) for feature in features_str]
  58. targets_float = [float(target) for target in targets_str]
  59. print(features_float)
  60. print(targets_float)
  61. # [0.1, 0.2, 0.3]
  62. # [1.0, 0.0, 1.0]
  63. # 应用7:与外部库和API交互
  64. # 使用外部库进行计算,需要浮点数输入
  65. import math
  66. radius = input("请输入圆的半径: ")
  67. radius_float = float(radius)
  68. area = math.pi * radius_float ** 2
  69. print(f"圆的面积是: {area}")
  70. # 请输入圆的半径: 5
  71. # 圆的面积是: 78.53981633974483
  72. # 应用8:处理特殊浮点数值
  73. # 正无穷大
  74. positive_infinity = float('inf')
  75. print(positive_infinity > 1e308)
  76. # 负无穷大
  77. negative_infinity = float('-inf')
  78. print(negative_infinity < -1e308)
  79. # 非数字(NaN)
  80. not_a_number = float('nan')
  81. print(not_a_number == not_a_number) # 输出: False,因为NaN不等于任何值,包括它自己
  82. # True
  83. # True
  84. # False
  85. # 应用9:传感器数据和物联网
  86. import random
  87. import time
  88. class Sensor:
  89. def get_temperature(self):
  90. """模拟从传感器获取温度数据"""
  91. # 在这里,我们使用随机数来模拟温度数据
  92. # 在实际应用中,你将从真实的传感器硬件读取数据
  93. return random.uniform(20.0, 30.0)
  94. class IoTPlatform:
  95. def send_data(self, data):
  96. """模拟将数据发送到物联网平台"""
  97. print(f"Sending data to IoT platform: {data}")
  98. # 在实际应用中,你将使用API或其他方法将数据发送到真实的物联网平台
  99. def main():
  100. sensor = Sensor()
  101. iot_platform = IoTPlatform()
  102. while True:
  103. # 从传感器获取温度数据
  104. temperature = sensor.get_temperature()
  105. # 处理数据(在这个例子中,我们只是简单地打印它)
  106. print(f"Sensor data: {temperature}°C")
  107. # 将数据发送到物联网平台
  108. iot_platform.send_data(temperature)
  109. # 等待一段时间再次读取数据(例如,每秒读取一次)
  110. time.sleep(1)
  111. if __name__ == "__main__":
  112. main()
  113. # Sensor data: 24.35495124775737°C
  114. # Sending data to IoT platform: 24.35495124775737
  115. # Sensor data: 28.555489228660974°C
  116. # Sending data to IoT platform: 28.555489228660974
  117. # Sensor data: 26.19175109694158°C
  118. # Sending data to IoT platform: 26.19175109694158
  119. # Sensor data: 23.442842394488252°C
  120. # Sending data to IoT platform: 23.442842394488252
  121. # Sensor data: 23.305392333104482°C
  122. # Sending data to IoT platform: 23.305392333104482
  123. # Sensor data: 28.318920293397788°C
  124. # Sending data to IoT platform: 28.318920293397788
  125. # Sensor data: 23.241318381210696°C
  126. # Sending data to IoT platform: 23.241318381210696
  127. # Sensor data: 22.93194419578242°C
  128. # Sending data to IoT platform: 22.93194419578242
  129. # Sensor data: 29.633611786850796°C
  130. # Sending data to IoT platform: 29.633611786850796
  131. # Sensor data: 28.789525652001835°C
  132. # Sending data to IoT platform: 28.789525652001835
  133. # Sensor data: 25.842904627594955°C
  134. # Sending data to IoT platform: 25.842904627594955
  135. # Sensor data: 25.638808282557044°C
  136. # Sending data to IoT platform: 25.638808282557044
  137. # Sensor data: 29.32173050465657°C
  138. # Sending data to IoT platform: 29.32173050465657
  139. # Sensor data: 20.165195824226277°C
  140. # Sending data to IoT platform: 20.165195824226277
  141. # Sensor data: 20.737832006367668°C
  142. # Sending data to IoT platform: 20.737832006367668
  143. # Sensor data: 23.253924015751473°C
  144. # Sending data to IoT platform: 23.253924015751473
  145. # Sensor data: 26.033152180188562°C
  146. # Sending data to IoT platform: 26.033152180188562
  147. # Sensor data: 24.672172262270546°C
  148. # Sending data to IoT platform: 24.672172262270546
  149. # Sensor data: 26.962446018513837°C
  150. # Sending data to IoT platform: 26.962446018513837
  151. # Sensor data: 25.624841852560117°C
  152. # Sending data to IoT platform: 25.624841852560117
  153. # Sensor data: 29.199482223621086°C
  154. # Sending data to IoT platform: 29.199482223621086
  155. # Sensor data: 24.938208577744142°C
  156. # Sending data to IoT platform: 24.938208577744142
  157. # Sensor data: 25.07537748796064°C
  158. # Sending data to IoT platform: 25.07537748796064
  159. # Sensor data: 25.582631737952596°C
  160. # Sending data to IoT platform: 25.582631737952596
  161. # Sensor data: 21.61064928872831°C
  162. # Sending data to IoT platform: 21.61064928872831
  163. # Sensor data: 28.000556528836086°C
  164. # Sending data to IoT platform: 28.000556528836086
  165. # Sensor data: 28.933697871076326°C
  166. # Sending data to IoT platform: 28.933697871076326
  167. # Sensor data: 29.74153889499609°C
  168. # Sending data to IoT platform: 29.74153889499609
  169. # Sensor data: 20.99602526908245°C
  170. # Sending data to IoT platform: 20.99602526908245
  171. # Sensor data: 27.63964636272862°C
  172. # Sending data to IoT platform: 27.63964636272862
  173. # Sensor data: 21.41344014798931°C
  174. # Sending data to IoT platform: 21.41344014798931
  175. # Sensor data: 26.64427259178116°C
  176. # Sending data to IoT platform: 26.64427259178116
  177. # Sensor data: 24.152113173007525°C
  178. # Sending data to IoT platform: 24.152113173007525
  179. # Sensor data: 26.14512555533888°C
  180. # Sending data to IoT platform: 26.14512555533888
  181. # Sensor data: 25.971064930704543°C
  182. # Sending data to IoT platform: 25.971064930704543
  183. # Sensor data: 24.002933526454864°C
  184. # Sending data to IoT platform: 24.002933526454864
  185. # Sensor data: 26.077168612363753°C
  186. # Sending data to IoT platform: 26.077168612363753
  187. # Sensor data: 20.005298691687234°C
  188. # Sending data to IoT platform: 20.005298691687234
  189. # Sensor data: 27.461037061526184°C
  190. # Sending data to IoT platform: 27.461037061526184
  191. # Sensor data: 29.799532289131108°C
  192. # Sending data to IoT platform: 29.799532289131108
  193. # Sensor data: 21.17595165361092°C
  194. # Sending data to IoT platform: 21.17595165361092
  195. # Sensor data: 26.913967154058682°C
  196. # Sending data to IoT platform: 26.913967154058682
  197. # Sensor data: 21.580229758211587°C
  198. # Sending data to IoT platform: 21.580229758211587
  199. # Sensor data: 23.072731413594248°C
  200. # Sending data to IoT platform: 23.072731413594248
  201. # Sensor data: 29.28654951908315°C
  202. # Sending data to IoT platform: 29.28654951908315
  203. # Sensor data: 20.907595373780616°C
  204. # Sending data to IoT platform: 20.907595373780616
  205. # Sensor data: 25.894732235004494°C
  206. # Sending data to IoT platform: 25.894732235004494
  207. # Sensor data: 24.4413952471027°C
  208. # Sending data to IoT platform: 24.4413952471027
  209. # Sensor data: 25.184028057117537°C
  210. # Sending data to IoT platform: 25.184028057117537
  211. # Sensor data: 24.660401385577995°C
  212. # Sending data to IoT platform: 24.660401385577995
  213. # Sensor data: 24.64640801088878°C
  214. # Sending data to IoT platform: 24.64640801088878
  215. # Sensor data: 27.622157701248156°C
  216. # Sending data to IoT platform: 27.622157701248156
  217. # Sensor data: 23.365817094313858°C
  218. # Sending data to IoT platform: 23.365817094313858
  219. # Sensor data: 27.07588598861893°C
  220. # Sending data to IoT platform: 27.07588598861893
  221. # Sensor data: 29.92619272137349°C
  222. # Sending data to IoT platform: 29.92619272137349
  223. # Sensor data: 29.593069159742917°C
  224. # Sending data to IoT platform: 29.593069159742917
  225. # Sensor data: 22.63983821332481°C
  226. # Sending data to IoT platform: 22.63983821332481
  227. # Sensor data: 24.65212426204157°C
  228. # Sending data to IoT platform: 24.65212426204157
  229. # Sensor data: 25.30846116751177°C
  230. # Sending data to IoT platform: 25.30846116751177
  231. # Sensor data: 23.188759041615153°C
  232. # Sending data to IoT platform: 23.188759041615153
  233. # Sensor data: 21.07982492326462°C
  234. # Sending data to IoT platform: 21.07982492326462
  235. # Sensor data: 20.11731887149259°C
  236. # Sending data to IoT platform: 20.11731887149259
  237. # Sensor data: 22.473766999092575°C
  238. # Sending data to IoT platform: 22.473766999092575
  239. # Sensor data: 26.39400136262076°C
  240. # Sending data to IoT platform: 26.39400136262076
  241. # Sensor data: 28.713549876825084°C
  242. # Sending data to IoT platform: 28.713549876825084
  243. # Sensor data: 22.169261944188865°C
  244. # Sending data to IoT platform: 22.169261944188865
1-2、VBA:
略
2、相关文章:

2-1、Python-VBA函数之旅-bytes()函数 

2-2、Python-VBA函数之旅-callable()函数

2-3、Python-VBA函数之旅-classmethod()函数 

2-4、Python-VBA函数之旅-compile()函数 

Python算法之旅:Algorithm

Python函数之旅:Functions

个人主页:https://blog.csdn.net/ygb_1024?spm=1010.2135.3001.5421
文章知识点与官方知识档案匹配,可进一步学习相关知识
算法技能树首页概览60609 人正在系统学习中
遨游码海,我心飞扬
微信名片
注:本文转载自blog.csdn.net的神奇夜光杯的文章"https://myelsa1024.blog.csdn.net/article/details/137886313"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2024 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top