Python 中如何将浮点转换为整数

我们在 Python 中使用了不同的数值数据类型,在本教程中,我们将学习如何将浮点值转换为整数值。

让我们看看实现相同的方法-

  1. 使用 trunc()
  2. 使用楼层()
  3. 使用 ceil()
  4. 使用 int()

所以,让我们从第一个开始-

使用 trunc()

下面的程序展示了如何在 Python 中使用 trunc() 将浮点值转换为整数。


#import trunc
from math import trunc
#initialising the values
a = 20.33
b = 12.46
c = 9.54
res_sum = a + b + c
#displaying the sum value
print("The result of a + b + c is ", res_sum)
#using trunc
print("The converted value of a is: ", trunc(a))
print("The converted value of b is: ", trunc(b))
print("The converted value of c is: ", trunc(c))
print("The converted value of sum is: ", trunc(res_sum))

输出:

The result of a + b + c is 42.33
The converted value of a is: 20
The converted value of b is: 12
The converted value of c is: 9
The converted value of sum is: 42

解释-

是时候了解上面的程序发生了什么-

  1. 既然要用 trunc(),我们第一步就导入了数学。
  2. 之后,我们初始化了三个浮点值,然后计算它们的总和。
  3. 最后,我们传递了变量 trunc() 中的 a、b、c 和 res_sum 来获得整数值。
  4. 在执行程序时,我们获得了所需的输出。

在下一个程序中,我们将使用 floor()。

使用地板()

首先,让我们理解当我们在 floor()中传递一个浮点值时会发生什么?

当一个浮点数传入 floor() 时,它将该数向下舍入到最接近的整数。

考虑下面给出的程序,


#import floor
from math import floor
#initialising the values
a = 20.33
b = 12.46
c = 9.54
res_sum = a + b + c
#displaying the sum value
print("The result of a + b + c is ", res_sum)
#using floor
print("The converted value of a is: ", floor(a))
print("The converted value of b is: ", floor(b))
print("The converted value of c is: ", floor(c))
print("The converted value of sum is: ", floor(res_sum))

输出:

The result of a + b + c is 42.33
The converted value of a is: 20
The converted value of b is: 12
The converted value of c is: 9
The converted value of sum is: 42

解释-

让我们看一下这个节目的解说。

  1. 既然要用 floor(),第一步就导入了数学。
  2. 之后,我们初始化了三个浮点值,然后计算它们的总和。
  3. 最后,我们在楼层()传递了变量 a、b、c、和 res_sum 得到整数值。
  4. 在执行程序时,我们获得了所需的输出。

现在,我们将看到如何使用 ceil() 来做同样的事情。

使用天花板()

首先,让我们理解当我们在 ceil()中传递一个浮点值时会发生什么?

当一个浮点数传入 ceil(),时,它将该数四舍五入到最接近的整数。


#import ceil
from math import ceil
#initialising the values
a = 20.33
b = 12.46
c = 9.54
res_sum = a + b + c
#displaying the sum value
print("The result of a + b + c is ", res_sum)
#using ceil
print("The converted value of a is: ", ceil(a))
print("The converted value of b is: ", ceil(b))
print("The converted value of c is: ", ceil(c))
print("The converted value of sum is: ", ceil(res_sum))

输出:

The result of a + b + c is 42.33
The converted value of a is: 21
The converted value of b is: 13
The converted value of c is: 10
The converted value of sum is: 43

解释-

让我们了解一下我们在这个项目中做了什么。

  1. 既然要用 ceil(),第一步就导入了数学。
  2. 之后,我们初始化了三个浮点值,然后计算它们的总和。
  3. 最后,我们通过变量 ceil() 中的 a、b、c、和 res_sum 得到整数值。
  4. 在执行程序时,我们获得了所需的输出。

最后,在最后一个程序中,我们将使用最基本的方法将浮点值转换为整数,即使用 int() 。

使用 int()

下面给出的程序说明了如何在程序中使用它。


#initialising the values
a = 20.33
b = 12.46
c = 9.54
res_sum = a + b + c
#displaying the sum value
print("The result of a + b + c is ", res_sum)
#using int()
print("The converted value of a is: ", int(a))
print("The converted value of b is: ", int(b))
print("The converted value of c is: ", int(c))
print("The converted value of sum is: ", int(res_sum))

输出:

The result of a + b + c is 42.33
The converted value of a is: 20
The converted value of b is: 12
The converted value of c is: 9
The converted value of sum is: 42

解释-

  1. 我们已经初始化了三个浮点值,然后计算它们的总和。
  2. 最后,我们已经在 int() 中传递了变量 a、b、c、和 res_sum,以获得整数值。
  3. 在执行程序时,我们获得了所需的输出。

结论

在本教程中,我们学习了在 Python 中将浮点数转换为整数的有趣方法。


原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/242371.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-12-12 12:48
下一篇 2024-12-12 12:48

相关推荐

  • 如何输入三个整数,并输出最大值Python

    对于初学者来说,输入三个整数并输出它们的最大值可能是一个比较基础的问题。然而,它却包含了Python中许多基本知识点的应用,因此学习它可以让我们更好地理解Python编程语言。 一…

    编程 2025-04-29
  • 使用FFmpeg在Java中将MP3 URL转换为PCM

    本文介绍了使用FFmpeg在Java中将MP3 URL转换为PCM的具体步骤,以及相应代码示例。 一、准备工作 在使用FFmpeg之前,需要先安装FFmpeg,可以在官网(http…

    编程 2025-04-29
  • Python随机生成100内的10个整数

    本文将从以下几个方面详细阐述Python随机生成100内的10个整数: 一、random库介绍 在Python中,生成随机数可以使用random库。random库包括两种类型的函数…

    编程 2025-04-29
  • 如何将Oracle索引变成另一个表?

    如果你需要将一个Oracle索引导入到另一个表中,可以按照以下步骤来完成这个过程。 一、创建目标表 首先,需要在数据库中创建一个新的表格,用来存放索引数据。可以通过以下代码创建一个…

    编程 2025-04-29
  • 全能编程开发工程师必备技能——如何优化大整数的计算

    本文将会为你分享如何解决大整数计算问题,以9999999967为例,我们将从多个方面对其做详细阐述,并给出完整的代码示例。 一、大整数的表示方法 在计算机中,我们通常采用二进制数来…

    编程 2025-04-29
  • 整数的因子包含自身吗

    本篇文章将从数学概念的角度、常用算法的应用、程序实现的方法等多个方面,对整数的因子包含自身的问题进行详细阐述。 一、质因数分解法 将整数进行质因数分解,若分解结果中所有质因子的指数…

    编程 2025-04-29
  • Python如何将字符串1234变成数字1234

    Python作为一种广泛使用的编程语言,对于数字和字符串的处理提供了很多便捷的方式。如何将字符串“1234”转化成数字“1234”呢?下面将从多个方面详细阐述Python如何将字符…

    编程 2025-04-29
  • 如何将Java项目分成Modules并使用Git进行版本控制

    本文将向您展示如何将Java项目分成模块,并使用Git对它们进行版本控制。分割Java项目可以使其更容易维护和拓展。Git版本控制还可以让您跟踪项目的发展并协作开发。 一、为什么要…

    编程 2025-04-28
  • 如何将Python开发的网站变成APP

    要将Python开发的网站变成APP,可以通过Python的Web框架或者APP框架,将网站封装为APP的形式。常见的方法有: 一、使用Python的Web框架Django Dja…

    编程 2025-04-28
  • Python中的整数类型int类总览

    本文将从多个方面,对Python中的整数类型int类进行全面介绍和阐述。 一、数据类型及基本操作 在Python中,整数类型的数据类型为int。在Python3.x中,整数类型的范…

    编程 2025-04-28

发表回复

登录后才能评论