- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试获取一个数字在 c 中的小数位数:0.0001 -> 4 位小数,3,54235 -> 5 位小数,依此类推(如果你没有得到它,后面的数字的数量逗号。)我们的老师说这可以通过两种方式完成,使用字符串和不使用字符串。我想我会继续不使用字符串,因为我没有使用字符串的经验。
这就是我想出来的
int funzione1(float decimals){
int x=1,c=0,y=1;
while (x!=0){
if((decimals - y) > 0){
y = y / 10;
c++;
}else{
decimals = decimals - y;
}
if(decimals == 0)
x=0;
}
return c-1;
}
当调用该函数时,它应该返回我计算的小数位数,但它没有,实际上它陷入了无限循环。
此代码背后的想法是让数字“字符串”中的每个数字都为 0,然后检查总数是否为 0
3.456 c=0
0.456 c=1
0.056 c=2
0.006 c=3
0.000 返回 c
但这给我留下了两个问题 1 如何确定逗号前的数字数量,例如 5564.34234 此代码将不起作用,因为它会在完整数字为 0 之前计数到 8。因此不会返回正确的小数位数.2。我设计的代码不起作用。只是陷入了无限循环。我不知道循环的无限性在哪里创建。
如何让这段代码起作用?
附言。我在 Java 中发现了这篇关于这个问题的文章:How to find out how many decimals a number has? 但它使用的是字符串,我不喜欢那样,因为我不知道如何使用字符串。
编辑:这是我试过的另一段代码,当你输入大于 1 的数字时输出 50,如果数字小于 0(我不明白,不是一点点)无论如何这里是代码:
int funzione1(float decimals){
int i=0;
while(decimals!=((int)decimals)){
i++;
decimals=decimals*10;
}
return i;
}
最佳答案
如果你不关心四舍五入那么你不需要计算小数位数,你可以只计算二进制位数。这是因为 10 恰好包含 2 作为因子一次,因此 10^n 和 2^n 具有相同数量的 2 作为因子。计算二进制位数的最快方法是获取 float 的指数。
例如二进制0.001取3位小数表示0.125,0.0001取4位0.0625。
您可以获取值的小数部分并继续乘以 2 并删除整数,就像人们建议用 10 做的那样(它会给您相同的答案)。
或者您可以通过优化解决方案获得更多乐趣(places 函数完成大部分工作):
#include <math.h>
int saturateLeft (unsigned int n) {
n |= (n << 1);
n |= (n << 2);
n |= (n << 4);
n |= (n << 8);
n |= (n << 16);
return n;
}
int NumberOfSetBits(int i)
{
i = i - ((i >> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
return (((i + (i >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
}
int places (double num) {
int exponent;
float mantissa = frexp (num, &exponent);
/* The answer we are looking for is given by the
(number of bits used by mantissa) - the exponent.
*/
unsigned intMantissa = scalbnf (mantissa, 32);
/* Could also be got by doing:
intMantissa = *(unsigned *)&mantissa << 9;
*/
/* To work out how many bits the mantissa covered we
need no gaps in the mantissa, this removes any gaps.
*/
intMantissa = saturateLeft (intMantissa);
int bitCount = NumberOfSetBits (intMantissa);
/* bitCount could also be found like this:
intMantissa = ~intMantissa;
int bitCount = 32 - ilogb (intMantissa) - 1;
*/
int result = bitCount - exponent;
if (result < 0)
return 0;
return result;
}
发现了 bitCounting 算法 here .
关于c - 获取数字在c中的小数位数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28339780/
如何将整数类型转换为 double /浮点类型以显示小数点?例如,如果我想将数字转换为货币格式: 5 会变成 5.004.3 会变成 4.30 javascript 有什么东西可以用来做这种转换吗?
【版权声明】未经博主同意,谢绝转载!(请尊重原创,博主保留追究权) https://www.cnblogs.com/cnb-yuchen/p/18107586 出自【进步*于辰的博客】
我意识到这是一个重复的问题,但是 this 中提到的解决方案这个问题完全不适合我。 我目前的代码如下 Sub ConvertTextToNumber() Dim Area As Range, C As
我正在使用数学 javascript,但在用它来替换点的逗号和逗号的点时遇到了一些麻烦。我可以改变千位分隔符的逗号,但无法设法将小数点变成逗号。我尝试了其他帖子中的一些建议,但没有感到高兴。目标是实现
我正在尝试在 Android 中创建一个数字选择器,但轮子只增加 1。我想增加 0.1。我在网上查了一下,但我发现了一个格式化的浮点数组禁用了轮子。请帮助并为语法感到抱歉,我正在学习。 最佳答案 您可
我正在尝试在多个网站上获取利率。数据相当非结构化,但形式足够接近。我想要捕捉的内容: x.xx% 至 xx.xx% 数据示例: 由 FDIC 成员 WebBank 发放的所有贷款。您的实际利率取决于信
在 MySQL 表中,我有一个具有不同值的 VARCHAR 列,这些值可能代表字符串、整数、浮点、任意值。这些值作为特定于语言的字符串写入数据库,这意味着 123.45 的浮点值可以写为德语中的 "1
我想编写一个正则表达式,它允许整数或具有 0 - 2 个小数位的小数。 有效输入 1 1. 1.1 1.11 111111111 111111111. 111111111.1 111111111.11
我正在尝试为 nullable 实现客户端验证其小数点分隔符可以是逗号(例如:123,45)。 在我看来: ... @Html.LabelFor(model => model.Turnove
我找不到合适的正则表达式来仅从字符串中提取 float 。考虑以下字符串: $string = "8x2.1 3x2"; 我想提取 2.1,我尝试了以下操作,但这给了我整数和 float : preg
我希望使用正则表达式函数分离以下数据,如下所示: 要使用的功能: let fx=(text,regex)=> Web.Page( " var x='
我是 jquery 新手。我有一个带有两个输入框的表单。我实现了一些验证。 Min.Amount Max.Amount
我正在java中实现一个简单的算法,它接受一个整数数组,并查找并返回数组中相邻整数的最大乘积。 为此,我首先初始化了一个名为largestProduct的变量,我用它来跟踪当前找到的最大(最佳)产品。
在 JavaScript 中,我想定义小数点的位置。我只能在示例中真正展示它。 假设输入值为 1234 。 我希望输出为 123.4 。 或者,如果输入是 12345 ,我希望输出是 123.45 。
我有这段代码,只允许在 keypress() 的输入字段中输入数字 if (e.which != 8 && e.which != 0 && (e.which 57)) { return fa
我目前正在开发一些基于 Django 的 Web 项目,在这个 Web 开发过程中,我遇到了以下我无法正确理解的代码。 if price_product['price'] == Decimal('-1
这个问题在这里已经有了答案: How do I print a double value with full precision using cout? (17 个答案) 关闭 7 年前。 我试图在
这应该是微不足道的,但我正在兜圈子,也许有人可以提供帮助。 我有两个表(T1,T2),我希望从中提取每行中的多个值并更新第三个表(T3)的内容当且仅当)T1 中有两个 UQ,NN 字段,T2 匹配,在
如果数字不是十进制,我需要附加.00,但是当我尝试下面的代码时,它会将整个数字更改为0.00。例如,如果数字是12,200,它会将其更改为0.00,而不是在末尾添加.00 $('.total-amou
我正在尝试在容器 View 中设置 9:16 纵横比 View 。以下代码在 viewDidLayoutSubviews 中设置约束,以便在正确的位置考虑自动布局。它还调用 layoutIfNeede
我是一名优秀的程序员,十分优秀!