简体   繁体   中英

Getting the first decimal place of a number

I have a number for example: 2.4444444 . I need to get first digit after the dot -- in my case it's 4 . How to implement it?

怎么样

( (int)(floor( fabs( num ) * 10 ) ) ) % 10

Chech these out! Tried it for you hope it helps

#include<stdio.h>
int main()
 {
  int b;
  float a;
  a=2.4444; 'load some value
  b=(int)a;  'typecast it into integer you get 2 into b variable
  a=a-b;     ' subtract b from a and you will get decimal point value 0.4444        
  a=a*10;    ' multiplying with 10 gives 4.444
  b=(int)a;  ' now apply the same logic again 
  printf("%d",b); 'outputs 4  
 }

Update written these function using these link

Extract decimal part from a floating point number in C

它可以很简单

num*10%10
(int)(num * 10) - ((int)num) * 10

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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