简体   繁体   English

如何打印数字的浮动部分的最后一位?

[英]How to print the last digit of floating part of a number?

I am finding an easy way to print the last floating point digit of a number. 我正在寻找一种打印数字的最后一个浮点数的简单方法。 For EG : 33.44 ans - 4 . For EG : 33.44 ans - 4

However, I managed it in JAVA easily, but I am a C/C++ beginner.An SSCCE for JAVA code : 不过,我设法它JAVA很容易,但我是一个C/C++ beginner.An SSCCE为JAVA代码:

import java.util.Scanner;

 public class Test{
 public static void main(String[] args){
 Scanner scan=new Scanner(System.in);
    Float f;
    f=scan.nextFloat();
    System.out.println(f);
    String g=f.toString(f);
    System.out.println(g.charAt(g.length()-1));
 }
}

I don't think it will be something that easy in C/C++ . 我认为在C/C++不会那么容易。 Any logics/solutions? 任何逻辑/解决方案?

If you want to do it in an equivalent manner, you would do something like (not compiled to check) 如果您想以等效方式进行操作,则可以执行类似操作(不进行编译以进行检查)

int getLast( double value )
{
  int rc;
  char buf[64];
  rc=snprintf(buf,64,"%f",value);
  // TODO:: check rc is in the range 1-64 
  return buf[rc-1]-'0';
}

You should probably check the result of rc and use strtol rather than the ascii hack of -'0' , but you get the picture. 您可能应该检查rc的结果,并使用strtol而不是-'0'的ascii hack,但是您得到了图片。

if you want the rightmost non-0 value, the return line should be replace with 如果您想要最右边的非0值,则返回行应替换为

char *x=&(buf[rc-1]);
while( *x=='0' ) x--;
return *x-'0';

If the nominal value of a floating-point number is not an integer, unless the fractional part is exactly equal to 0.5, 0.25, or 0.75, the exact value of the fractional part will always end in 125, 375, 625, or 875. In many cases, one isn't actually interested in the exact nominal value of the fractional part, but instead a rounded decimal representation. 如果浮点数的标称值不是整数,除非小数部分正好等于0.5、0.25或0.75,否则小数部分的确切值将始终以125、375、625或875结尾。在许多情况下,人们实际上对小数部分的精确名义价值不感兴趣,而是舍入为十进制的十进制表示形式。 In that case, the value of the "last" digit will depend upon how much one is willing to have the reported value differ from the number's exact nominal value (eg the 1.0f / 10.0f is a float whose nominal value is exactly 1,342,173/13,421,728, ie 0.100000001490116119384765625. The last digits of the exact nominal value are 625, but the number might also be interpreted as 0.1, or 0.1000000015, 0.10000000149, 0.10000000149012, etc. so one could justify 1, 5, 9, or 2 as being the "last" digit. 在那种情况下,“最后”数字的值将取决于多少人愿意使所报告的值与数字的实际标称值不同(例如1.0f / 10.0ffloat其标称值恰好为1,342,173 / 13,421,728,即0.100000001490116119384765625。准确标称值的最后一位数字为625,但该数字也可能解释为0.1或0.1000000015、0.10000000149、0.10000000149012等,因此可以将1、5、9或2证明为“最后一位”数字。

Hope this helps you(in C++) : 希望这对您有帮助(在C ++中):

#include <iostream>
#include<string>
using namespace std;
int main()
{
 string float_data;
 cin>>float_data;
 int len = float_data.length();
 cout<<float_data.at(len-1)<<endl;
  return 0;
}

The below approach can be followed but it has limitation that the float value should not be very big. 可以采用以下方法,但是它的局限性是浮点值不能太大。

void main()
{
int i, m;
float n;
printf("Enter the number n: ");
scanf("%f", &n);
m=(int)n;
i=(m % 10);
printf("last digit = %d", i);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM