从C中的浮点数中提取小数部分

时间:2009-02-01 00:53:06

标签: c floating-point decimal numbers

我们如何提取浮点数的小数部分并将小数部分和整数部分存储到两个单独的整数变量中?

14 个答案:

答案 0 :(得分:81)

您使用modf功能:

double integral;
double fractional = modf(some_double, &integral);

您也可以将其强制转换为整数,但要注意您可能会溢出整数。结果是不可预测的。

答案 1 :(得分:41)

试试这个:

int main() {
  double num = 23.345;
  int intpart = (int)num;
  double decpart = num - intpart;
  printf("Num = %f, intpart = %d, decpart = %f\n", num, intpart, decpart);
}

对我来说,它产生:

Num = 23.345000, intpart = 23, decpart = 0.345000

这似乎是你所要求的。

答案 2 :(得分:13)

快速“坚果壳”中最明显的答案似乎是:

#define N_DECIMAL_POINTS_PRECISION (1000) // n = 3. Three decimal points.

float f = 123.456;
int integerPart = (int)f;
int decimalPart = ((int)(f*N_DECIMAL_POINTS_PRECISION)%N_DECIMAL_POINTS_PRECISION);

您可以通过更改N_DECIMAL_POINTS_PRECISION来更改所需的小数点数。

答案 3 :(得分:1)

我使用double float创建了一个子例程,它返回2个整数值。


void double2Ints(double f, int p, int *i, int *d)
{ 
  // f = float, p=decimal precision, i=integer, d=decimal
  int   li; 
  int   prec=1;

  for(int x=p;x>0;x--) 
  {
    prec*=10;
  };  // same as power(10,p)

  li = (int) f;              // get integer part
  *d = (int) ((f-li)*prec);  // get decimal part
  *i = li;
}

void test()
{ 
  double df = 3.14159265;
  int   i,d;
  for(int p=2;p<9;p++)
  {
    double2Ints(df, p, &i,&d); printf("d2i (%d) %f = %d.%d\r\n",p, df,i,d);
  }
}

答案 4 :(得分:1)

即使我在想如何做到这一点。但我找到了办法。 试试这段代码

printf("Enter a floating number");
scanf("%d%c%d", &no, &dot, &dec);
printf("Number=%d Decimal part=%d", no, dec);

输出: -

Enter a floating number
23.13
Number=23 Decimal part=13

答案 5 :(得分:0)

这是另一种方式:

#include <stdlib.h>
int main()
{
    char* inStr = "123.4567";         //the number we want to convert
    char* endptr;                     //unused char ptr for strtod

    char* loc = strchr(inStr, '.');
    long mantissa = strtod(loc+1, endptr);
    long whole = strtod(inStr, endptr);

    printf("whole: %d \n", whole);     //whole number portion
    printf("mantissa: %d", mantissa);  //decimal portion

}

http://codepad.org/jyHoBALU

输出:

whole: 123 
mantissa: 4567

答案 6 :(得分:0)

如果您只想获得第一个十进制值,解决方案非常简单。

以下是一个解释性示例:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="sum_table" border="1">
					<tr class="titlerow">
						<td align="center">plan1
					</tr>
					<tr class="titlerow">
						<td align="center">EUR					
					</tr>
					<tbody>
	        <tr>		
							<td class="rowDataSd" align="right" nowrap>2 358,94
         </tr>
         <tr>
					    <td class="rowDataSd" align="right" nowrap>3
         </tr>
         <tr>	
             <td class="totalCol"></td>
        </tr>		
</tbody></table>

比方说,我们的初始浮点值为27.8。

  • 只需将初始浮点值转换为int,就可以丢弃小数部分,只保留整数部分。
  • 通过将初始浮点值乘以10得到278.0的结果,然后将此结果转换为int,得到值278
  • 如果我们将278除以10,我们得到27.8,其余为8,这是小数点右侧的值。因此使用模数。

然后,可以使用此技术通过使用例如100而不是10来获取以下十进制字符,依此类推。

请注意,如果您在实时系统上使用此技术,例如将其显示在7段显示器上,它可能无法正常工作,因为我们乘以浮点值,其中乘法需要很多开销时间。

答案 7 :(得分:0)

             //import { PlayerService } from './player.service'; //remove this line.. you don't need this import of a service class since this is a model class

                export class PlayerNationality {
                    public id: number;
                    public name: String;
                    public last: String;
                    public nationality: String;

//note this method will change the case to uppercase of "name", "last","nationality" 

                toUpperCase(){
                   this.name=this.name.toUpperCase();
                   this.last=this.last.toUpperCase(); 
                   this.nationality=this.nationality.toUpperCase();
                }       
            }

答案 8 :(得分:0)

假设A是您的整数,然后是(int)A,表示将数字转换为整数并将成为整数部分,另一个是(A-(int)A)* 10 ^ n,这里n是整数保留小数点。

答案 9 :(得分:0)

使用浮点数减去 floor 值即可得到其小数部分:

double fractional = some_double - floor(some_double);

这可以防止将类型转换为整数,如果浮点数太大而导致整数值甚至不能包含它,则可能会导致溢出

对于负值,此代码还为您提供了浮点数的正小数部分,因为 floor() computes the largest integer value not greater than the input value

答案 10 :(得分:0)

float num;
int intgPart;
float fracPart;
printf("Enter the positive floating point number: ");
scanf("%f", &num);

intgPart = (int)num;
fracPart = num - intgPart;

小数部分可以通过原始双精度值减去整数部分得到。

答案 11 :(得分:-1)

最好的想法是在数据采用String格式时解决问题。如果您将数据作为String,则可以根据小数点对其进行解析。您将积分和小数部分提取为子串,然后将这些子串转换为实际整数。

答案 12 :(得分:-1)

我做了这个功能,似乎工作正常:

#include <math.h>

void GetFloattoInt (double fnum, long precision, long *pe, long *pd)
{
  long pe_sign;
  long intpart;
  float decpart;

  if(fnum>=0)
  {
    pe_sign=1;
  }
  else
  {
    pe_sign=-1;
  }

  intpart=(long)fnum;
  decpart=fnum-intpart;

  *pe=intpart;  
  *pd=(((long)(decpart*pe_sign*pow(10,precision)))%(long)pow(10,precision));
}

答案 13 :(得分:-1)

cout<<"enter a decimal number\n";
cin>>str;
for(i=0;i<str.size();i++)
{
    if(str[i]=='.')
    break;
}

for(j=i+1;j<str.size();j++)
{
    cout<<str[j];
}
相关问题