• C

EASY - how do you printout .1 instead of 0.1

Simple, stupid question
long bleh = .1
printf("%f\n", bleh)
prints out 0.1
i need:
printf("%<something_here>f\n", bleh)
so that the end output is just .1 instead of 0.1. If I can't get this to work I will have to figure out if my value is between 0 and 1 and then parse it using some sort of 'Right' function... much more work than i think i need.
Thanks


jramrusAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
grg99Connect With a Mentor Commented:

I don't see a format spec for this, but you could:

char Buf[50];

snprintf( Buf, sizeof( Buf ), "%f", bleh );

printf( "%s", &Buf[ Buf[0] == '0' && Buf[1] == '.'  ] );

0
 
jramrusAuthor Commented:
to make sure the solution doesn't work for just this case, here are several examples
.000001
1.00000
99999.9

so the precision depends on the value

Possibly there is a better way to do take a double and convert it to a character array than sprintf where the 0. in 0.1 isn't automatically put in?
0
 
jramrusAuthor Commented:
my current, really ugly solution

char* seven_total_characters(double myval)
// returns 7 characters for any input > 0 and < 99999.9
{
      char* temp, *temp2;
      temp = (char*)malloc(7);
      temp2 = (char*)malloc(strlen(temp)*sizeof(char));
      if(myval < 1)
      {
            sprintf(temp, "%0.6f", myval);
            sprintf(temp2, "%s", temp+1);
            strcpy(temp, temp2);
      }
      else
      {
            if(myval < 10)
                  sprintf(temp, "%0.5f", myval);
            else if(myval < 100)
                  sprintf(temp, "%0.4f", myval);
            else if(myval < 1000)
                  sprintf(temp, "%0.3f", myval);
            else if(myval < 10000)
                  sprintf(temp, "%0.2f", myval);
            else if(myval < 100000)
                  sprintf(temp, "%0.1f", myval);
      }      
      free(temp2);
      return temp;
}

any improvements that anyone can see? if i need 8 characters, the entire thing changes. i'm sure there is a better way to do it.
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
grg99Commented:
So what's wrong with my solution above?  :)

0
 
sunnycoderConnect With a Mentor Commented:
I assume that you have two arguments. Buffer is large enough to sprintf the biggest double

char * remove_leading_zeros ( double myval, char * buffer )
{
         char * temp;
         sprintf ( buffer, "%lf", myval );
         temp = buffer;
         while ( *temp == '0' && *temp != '.' && *temp != '\0' )
                  temp++;
         return temp;
}

you can easily add range checks for making sure that value is between 0 and 99999.9 or any other number you desire. This will give you any amount of precision you desire. If you want only 7 digits, you can strncpy from temp to temp+7 and return that value, you do not have to add a check for all possible value ranges.

cheers
0
 
dtvjhoCommented:
Your first line:
long bleh = .1
is suspicious. What compiler are you using? My Solaris Forte cc is ANSI and it truncates .1 thereby assigning zero to my long.

Regarding
printf("%<something_here>f\n", bleh)
you could use %.1f and this will yield "0.1". To drop the leading zero you could use sprintf followed by printf:

sprintf(buffer, "%.1f", bleh);
printf("%s\n", &buffer[1]);

Yes, this is a narrow solution as it would not handle input values with more than one digit on either side of the decimal. Let us know if you expect values that don't fit this pattern.
0
 
jramrusAuthor Commented:
grg99.... i must admit, i could not get snprintf to work at all. I will look into this further when i get a chance.

sunnycoder, unless i'm using it wrong, an input of '99999.0' yields '9999.000000'. THE most important characteristic of the result is that it is 7 total characters long.

dtvjho, my appologies, as you can see from the code I was using I changed it from a long to a double. Using %.1f, as you already know, would not work for many cases, as can be shown by my code that had to do this
          if(myval < 10)
               sprintf(temp, "%0.5f", myval);
          else if(myval < 100)
               sprintf(temp, "%0.4f", myval);
          else if(myval < 1000)
               sprintf(temp, "%0.3f", myval);
          else if(myval < 10000)
               sprintf(temp, "%0.2f", myval);
          else if(myval < 100000)
               sprintf(temp, "%0.1f", myval);

It appears that I am not going to get the trivial solution I wanted. Possibly a better approach is to just throw my double into a string(just using %f), and then take the string and manually pad it with 0's until I get the final result to be the right length. I think I can do that in very few lines, and it would work for all cases. Thoughts?
0
 
jramrusAuthor Commented:
actually sunnycoder, your solution might work better than i first thought
0
 
grg99Commented:
sunny's while condition:

 *temp == '0' && *temp != '.' && *temp != '\0'


looks mighty peculiar.   if the first part is true (*temp == '0'), then the second and third parts HAVE to be true.  

If the first part is false,then the other two parts, due to the "&&", are never evaluated.

I humbly submit that my clever entry does very much the same thing, but without the complexity and excess baggage of the above.

0
 
sunnycoderCommented:
Clever code grg99 ... :) ... needs someone like you to come up with that

However, is it guaranteed that true would always evaluate to 1 ... Isn't 2 or 3 or anything non zero, a perfectly valid true value ?

jramrus ,

>sunnycoder, unless i'm using it wrong, an input of '99999.0' yields '9999.000000'. THE most
>important characteristic of the result is that it is 7 total characters long.

char * remove_leading_zeros ( double myval, char * buffer )
{
         char * temp;
         sprintf ( buffer, "%lf", myval );
         temp = buffer;
         while ( *temp == '0' && *temp != '.' && *temp != '\0' )
                  temp++;
         temp+7='\0';  /*adding a string terminator character after 7 characters.*/
         return temp;
}

make sure that you pass valid buffer to the function and free it in the calling program (if you malloced it, that is) after you have finished using it.
0
 
grg99Commented:

>However, is it guaranteed that true would always evaluate to 1 ...

The result of a comparison operator always results in 0 or 1.


> Isn't 2 or 3 or anything non zero, a perfectly valid true value ?

True also.  A somewhat debatable decision that Dennis Richie musta made at 3 AM someday which has reverberated down thru the ages.  Estimated cost so far:  $600,000,000.  But it saves a little typing.


0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.