2

I'm programming in C language and I'm in doubt with the conversion of floating values.

I have this summarized code that works perfectly:

float number = get_float("Number: ");
float convert= number * 100; 
printf("Number: %g\n", convert);

Using printf with %g I can see the number without the zeros and points.

Has I how to get the same result of printf with %g, but concatenating in the float convert variable and get the conversion directly ?

Output:

I have it:

0.70 ----> 70.000000

I need it:

0.70 ----> 70

Thank you

Gajanan Kulkarni
  • 697
  • 6
  • 22

3 Answers3

2

Read the documentation of print and specifier %f

printf("Number: %.0f\n", convert);
// print floating point with no point and 0
// eg : printf("Number: %.0f\n", 70.0000); -> 70
Ptit Xav
  • 3,006
  • 2
  • 6
  • 15
1

What you need is a int. int does not have a decimal part. Try this

int convert= number * 100;

zeitgeist
  • 852
  • 12
  • 19
0

Casting should do the trick. Here is modified version of ur code which works as required:

#include <stdio.h>
#include <stdlib.h>

int main()
{
  // getting float
  float number = get_float("Number: ");

  // casting to int to get rid of fractional part
  int convert = (int)(number * 100);

  // printing to check
  printf("Number: %d\n", convert);
  
  return (0);
}
Criss Hills
  • 189
  • 1
  • 12