I have an array of floats: float net_pay[5] . Each element is a decimal number ie. 999.99, 35.42, 318.34 etc . What I would like to do is to scan the decimal portion, in first case it would be 99 to a variable cents. I know how to do it when the floats have always the same number of digits, however, in this case they are inconsistent. Anyone willing to explain or provide a link to a tutorial of some sort that would explain it to me?

Thanks in advance

Recommended Answers

All 14 Replies

Try dividing by 10 until the number as an int equals 0.

float temp = net_pay[i];

while ( (int)temp > 0 ) {
  temp /= 10;
}

printf( "Before the radix -- %f\n", (float)(int)net_pay[i] );
printf( "After the radix -- %f\n", temp );

Try dividing by 10 until the number as an int equals 0.

float temp = net_pay[i];

while ( (int)temp > 0 ) {
  temp /= 10;
}

printf( "Before the radix -- %f\n", (float)(int)net_pay[i] );
printf( "After the radix -- %f\n", temp );

yes, I have tried something similar but the output still is "x.xx" instead of just"xx". I need it that way because I'm going to put it between strings and that 0. will mess it up.

use modf() to extract signed integral and fractional values, then multiply the fractional part by 100 and assign it to an integer

#include <math.h>

<snip>
float fractional_part;
float integer_part;
float number = 999.99;

fractional_part = modf(number, &integer_part);
int cents = (int)(fractional_part * 100.0);

use modf() to extract signed integral and fractional values, then multiply the fractional part by 100 and assign it to an integer

#include <math.h>

<snip>
float fractional_part;
float integer_part;
float number = 999.99;

fractional_part = modf(number, &integer_part);
int cents = (int)(fractional_part * 100.0);

Thanks dragon, first time I heard of such thing but looks good. Will try in a sec and post if it works.

okay, thanks to modf I can get what I wanted but:

1) when I run the prog, the compiler says that the stack around the variable integer_part was corrupted. Although when I hit ignore everything is ok so I dunno.
2. this was it won't round the decimal. For ex, the number is 333.467. after modf and print it shows 46 as cents instead of 47 which should be after rounding.
Anyone?

1) post code. I can't see your monitor from where I sit :)

2) check the 3d decimal place greater than 5. This will probably work, although tested only with the Vista Calculator program.

if( (fractional_part * 100.0) >= (float)cents + 0.5F)
          cents++;

1) post code. I can't see your monitor from where I sit :)

2) check the 3d decimal place greater than 5. This will probably work, although tested only with the Vista Calculator program.

if( (fractional_part * 100.0) >= (float)cents + 0.5F)
          cents++;

yes that fixed the rounding problem thanks a lot. Could you explain what F means in 0.5F?

a part of the code:

#include <stdio.h>
#include <io.h>
#include <math.h>

#define MAXA 5
void convert(short int Value);

void main(void)
{

	float net_pay[MAXA] = { 44, 22, 45.88, 333.467, 64.77 },
		  integer_part, fractional_part;
	short int x = net_pay[4], Val=0, i=1;
	int cents;

	printf("The Sum ");
	
	fractional_part = modf(net_pay[4], &integer_part);

	cents = (int)(fractional_part * 100.0);
	if( (fractional_part * 100.0) >= (float)cents + 0.5F)
	{
       cents++;
	}
	printf("and %d/100 Dollars\n\n", cents);
}

there are parts in between that I cut out but are irrelevant because compiler complains only about integer_part variable

>> Could you explain what F means in 0.5F?

F = Float. Without it the default is double and some compilers will produce warning that attempting to assign a double to a float.

>> compiler complains only about integer_part variable
what are the compiler errors/warnings ?

>there are parts in between that I cut out but are irrelevant because compiler complains only about integer_part variable

Make the variables double instead of floats and you'll be fine.
BTW, main returns an int, so it should be int main( void ) and not
void main( void )

I didn't get any problems after fixing up the floats, doubles and shorts problems. Also note that main() ALWAYS returns an int. Declaring main() as void may be acceptable to some compilers but it is non-standard.

int main(void)
{

	float net_pay[MAXA] = { 44.0F, 22.0F, 45.88F, 333.467F, 64.77F },
		  integer_part = 0.0F, fractional_part = 0.0F;
    short int x = (short)net_pay[4], Val=0, i=1;
	int cents;

	printf("The Sum ");
	
	fractional_part = modf(net_pay[4], &integer_part);

	cents = (int)(fractional_part * 100.0);
	if( (fractional_part * 100.0) >= (float)cents + 0.5F)
	{
       cents++;
	}
	printf("and %d/100 Dollars\n\n", cents);
    return 0;
}

F = Float. Without it the default is double and some compilers will produce warning that attempting to assign a double to a float.

>> compiler complains only about integer_part variable
what are the compiler errors/warnings ?

thanks for clarification, the only way I knew to do it is put (float) before it.

as for the errors, not everything is smooth; I just had to change the variables to doubles.

Thanks everyone for all the input, much appriciated.

Best Regards
DeathEvil

thanks for clarification, the only way I knew to do it is put (float) before it.

as for the errors, not everything is smooth; I just had to change the variables to doubles.

Thanks everyone for all the input, much appriciated.

Best Regards
DeathEvil

In C the function modf() is always double. However you could use instead the function modff() which is for floats.

In C the function modf() is always double. However you could use instead the function modff() which is for floats.

thank you, good to know about the modff(). Again big thanks to everyone.

I'm trying to do the same thing as the OP, but I don't know how long my fractional_part is going to be. If I do

char *str;
float fractional_part;
sprintf( str, "%f", fractional_part );

, will I get trailing garbage on the end of str (e. g., 123.4555888233546 instead of 123.456)?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.