如何将一个数字转换为0.mynumber
How to convert a number to 0.mynumber
如何将数字转换为0 ?号码是多少?
int i = 50;
float a = 0.i //wrong code :D
还是别的什么?我该怎么做呢?
float a = i;
while( a >= 1.0f ) a /= 10.0f;
这是丑陋的,但我认为这是有效的:
int i = 50;
std::stringstream ss;
ss << "0." << i;
float a;
ss >> a;
怎么样:
#include <cmath>
#include <initializer_list>
#include <iostream>
float zero_dot( float m ) {
return m / pow( 10.0, floor( log( m ) / log( 10.0 ) ) + 1 );
}
int main() {
for( auto const & it: { 5.0, 50.0, 500.0, 5509.0, 1.0 } ) {
std::cout << it << ": " << zero_dot( it ) << std::endl;
}
return 0;
}
输出为:
5: 0.5
50: 0.5
500: 0.5
5509: 0.5509
1: 0.1
相关文章:
- 将数字转换为字母(例如:123 转换为一二三)
- 将数字转换为单词
- 使用if-else将数字转换为单词
- 将数字转换为填充字符串
- 函数在许多数字上转换为基数 1 时减去 2?
- 为什么将双精度转换为 int 似乎在第 16 位数字之后将其四舍五入?
- 如何将 UTF-8 文本从文件转换为某个可以迭代的容器,并检查每个符号是否为C++字母数字?
- 将 C 函数转换为 C++ 以检查数字是否有效
- 编译时间文本到数字转换 (atoi)
- 以C++格式将字符串转换为数字
- 在 c++ 中将数字转换为矢量数组的函数
- 奇怪的数字转换C++
- 我想使用 std::stoi 函数在 cpp 中将字符串转换为整数,因为我想在字符串中找到不同数字的总和(在下级酶中)
- 用C++将浮点数字转换为本地化字符串
- 我需要静态转换我的数字模板值吗
- 如何将boost::asio::ip::address_v6 ip转换为2个uint64_t数字,并从2个uint 64
- Rcpp - 用二进制函数转换数字向量?
- 我如何使用堆栈转换数字从基数10到基数2在c++
- 如何在c++中从字节数组中的整数转换数字
- c++程序转换数字的位数