Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 683
LightNight Avatar asked Jan 28 '26 06:01

LightNight


2 Answers

How about

( (int)(floor( fabs( num ) * 10 ) ) ) % 10
like image 195
Kiril Kirov Avatar answered Jan 30 '26 18:01

Kiril Kirov


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

How to extract the decimal part from a floating point number in C?

like image 40
niko Avatar answered Jan 30 '26 20:01

niko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!