Only Show Decimal Point If Floating Point Component Is Not .00 Sprintf/Printf

Only show decimal point if floating point component is not .00 sprintf/printf

You want to use %g instead of %f:

"%gx" % (factor / 100.00)

Use sprintf to format floats with no decimal places if integer

Yours is a problem of approximation.

Suppose that the percentage is 19.999. Then fractional_part would be 99, and the floating point branch would be invoked.

But printing 19.999 with two decimals will round it to 20.00, and that is what is printed.

You could always use the floating point branch, in order to get consistent results, and then truncate at '.' if it comes out with '.00'. Otherwise, you risk your test and printf's internals to be at odds some time.

#include <stdio.h>
#include <string.h>

int main(int argc, char **argv)
{
float percentage = 19.999;
char buffer[50];

for (percentage = 19.990; percentage < 20.001; percentage += 0.001)
{
sprintf(buffer, "%.2f", percentage);
char *p = strstr(buffer, ".00");
if (p) *p = 0x0;
printf("%.3f rendered as %.2f and becomes %s\n", percentage, percentage, buffer);
}
return 0;
}

19.990 rendered as 19.99 and becomes 19.99
19.991 rendered as 19.99 and becomes 19.99
19.992 rendered as 19.99 and becomes 19.99
19.993 rendered as 19.99 and becomes 19.99
19.994 rendered as 19.99 and becomes 19.99
19.995 rendered as 19.99 and becomes 19.99
19.996 rendered as 20.00 and becomes 20
19.997 rendered as 20.00 and becomes 20
19.998 rendered as 20.00 and becomes 20
19.999 rendered as 20.00 and becomes 20
20.000 rendered as 20.00 and becomes 20
20.001 rendered as 20.00 and becomes 20

If you don't agree with printf's rounding strategy, just use round() on (a copy of) percentage and force your own. Or you might also, e.g., sprintf() with three digits, and erase the third.

And in your specific case (note how my system (Linux x86_64) renders 0x419FFFFF):

#include <stdio.h>
#include <string.h>
#include <stdint.h>

int main(int argc, char **argv)
{
float percentage = 3.1415;
char buffer[50];

((uint32_t *)(&percentage))[0] = 0x419FFFFF;

sprintf(buffer, "%.2f", percentage);
char *p = strstr(buffer, ".00");
if (p) *p = 0x0;
printf("%.15f rendered as %.2f and becomes %s\n", percentage, percentage, buffer);
return 0;
}

19.999998092651367 rendered as 20.00 and becomes 20

Floating point value not being displayed using printf function

By default, the minimalistic printf library is used, which doesn't support floating point numbers and results in a "?" as a placeholder for the value. You have to tell the linker to use the floating point library.

For example (from GNU makefile)

PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min

You have to use the floating point version like so:

PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt

Depending on your base makefile, there should already be a section like this:

# If this is left blank, then it will use the Standard printf version.
PRINTF_LIB =
#PRINTF_LIB = $(PRINTF_LIB_MIN)
#PRINTF_LIB = $(PRINTF_LIB_FLOAT)

Just uncomment the line containing PRINTF_LIB_FLOAT.

EDIT:
If you use AvrStudio4 without a custom makefile do this:

  1. use Project - Configuration
  2. select icon ("libraries")
  3. highlight "libm.a" and add to the right panel
  4. do the same for "libprintf_flt.a"
  5. under 5th icon "custom"
  6. select "[Linker]" optins
  7. type in "-Wl,-u,vfprintf" and [add]

Two decimal places using printf( )

What you want is %.2f, not 2%f.

Also, you might want to replace your %d with a %f ;)

#include <cstdio>
int main()
{
printf("When this number: %f is assigned to 2 dp, it will be: %.2f ", 94.9456, 94.9456);
return 0;
}

This will output:

When this number: 94.945600 is assigned to 2 dp, it will be: 94.95

See here for a full description of the printf formatting options: printf

How to print a float with 2 decimal places in Java?

You can use the printf method, like so:

System.out.printf("%.2f", val);

In short, the %.2f syntax tells Java to return your variable (val) with 2 decimal places (.2) in decimal representation of a floating-point number (f) from the start of the format specifier (%).

There are other conversion characters you can use besides f:

  • d: decimal integer
  • o: octal integer
  • e: floating-point in scientific notation


Related Topics



Leave a reply



Submit