在 🦁 Python 中 🐘 :
python
使 🦄 用 🌳 str() 函数 🌼
float_value = 3.14159
string_value = str(float_value)
在 🌻 C++ 中:
cpp
include
int main() {
float float_value = 3.14159;
std::stringstream ss;
ss << float_value;
std::string string_value = ss.str();
return 0;
在 💐 Java 中 🐛 :
java
float float_value = 3.14159f;
String string_value = String.valueOf(float_value);
在 🦁 C 中 🌻 :
csharp
float float_value = 3.14159f;
string string_value = float_value.ToString();
在 🐈 JavaScript 中:
javascript
const float_value = 3.14159;
const string_value = float_value.toString();
include
int main() {
float f = 123.45;
char str[100];
// 使 🦉 用 sprintf() 函 🐬 数
sprintf(str, "%.2f", f);
// 输出结果 🪴
printf("浮 🐘 点 🐎 型 🦟 :%.2f\n", f);
printf("字符型 💮 :%s\n", str);
return 0;
java
import java.text.DecimalFormat;
public class Main {
public static void main(String[] args) {
float f = 123.45f;
String str = new DecimalFormat(".").format(f);
System.out.println("浮点 🦁 型:" + f);
System.out.println("字符 🦉 型 🦄 :" + str);
}Python
python
f = 123.45
str = f"{f:.2f}"
print("浮 🌺 点 🐒 型 🦟 :", f)
print("字 🐡 符 🐶 型:", str)
python
使用 🐒 内置函数 💮 int()
int_value = int(float_value)
使 🐧 用 math.trunc() 函 🦋 数 🦉
from math import trunc
int_value = trunc(float_value)
使用 // 运 🌹 算符
int_value = float_value // 1
使用类型转换运 🕸 算 🌸 符 🦅 :`(int)`
使 🐒 用 `std::round()` 或 `std::floor()` 函数 🐵 进行四舍五入或向下取整。
Python
使用 `int()` 函 🌸 数 🌲 :
python
my_float = 10.5
my_int = int(my_float) 输 🌷 出 10
使用 `math.floor()` 或 `math.ceil()` 函数 🌷 进行向下取整或向上 💮 取整:
python
import math
my_float = 10.5
my_int_floor = math.floor(my_float) 输 🐶 出 10
my_int_ceil = math.ceil(my_float) 输 🐳 出 🦁 11
使用 `Math.round()` 函 🐒 数进行四舍五入:
java
double myDouble = 10.5;
int myInt = Math.round(myDouble); // 输 🦟 出 🦢 11
使用 `(int)` 类型 💐 转换运算符进行 🐎 截断:
java
double myDouble = 10.5;
int myInt = (int) myDouble; // 输 🐒 出 🐛 10
使 🐱 用 `Math.Round()` 函数进行四 🦟 舍五入:
csharp
double myDouble = 10.5;
int myInt = (int)Math.Round(myDouble); // 输 🐎 出 🐈 11
使用 💮 `(int)` 类 🦍 型转换运算符进行截 🍁 断:
csharp
double myDouble = 10.5;
int myInt = (int)myDouble; // 输 🦍 出 🐧 10