Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Standard C, C++

Reply
 
LinkBack Thread Tools Display Modes
Old 10-07-2005, 08:05 PM   #1 (permalink)
ericthered13
Registered User
 
Join Date: Oct 2005
Posts: 1
ericthered13 is on a distinguished road
Round a Decimal withOUT the leading zero

Does anyone know how I could use printf to display a decimal number without the leading zero?

Something like,
{
...
x = .45;
printf ("%lf", x);
...
}
and have that display '.45' instead of '0.45'?

Last edited by ericthered13; 10-08-2005 at 12:53 PM.
ericthered13 is offline   Reply With Quote
Old 10-10-2005, 05:38 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
You can't not without some black magic, I think the printf() man page explains it quite well
Quote:
f, F
The floating arg is converted to decimal notation in the style [-]ddd.ddd, where the number of fractional digits is equal to the precision specification. If the precision is missing, it is taken as 6; if the precision is zero and the # flag is not specified, no decimal point character appears. If a decimal point character appears, at least one digit appears before it. The value is rounded to the appropriate number of digits.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-11-2005, 07:49 AM   #3 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
By black magic I'm thinking of something like this:
Code:
/*
 * bprint() converts a given double value in a way so
 * any value less than 1 will _not_ have leading zeroes
 *
 * buffer     - an allocated char* 
 *              where the return value will be stored
 * value      - the value intended for conversion
 * precission - the wanted precission setting, as
 *              described for any *printf function
 */

#define LENGTH 256

int bprint(char buffer[], double value, unsigned long int precission)
{
  char format[LENGTH +1], res[LENGTH +1];
  int i=0, j=0;
#ifdef HAVE_SNPRINTF
  /* create a format required by the user */
  if(!snprintf(format, LENGTH, "%%.%dlf", prescission))
    return -1;
  /* fill in the value in our prefered format */
  if(!snprintf(res, LENGTH, format, value))
    return 0;
#else
  if(!sprintf(format, "%%.%dlf", precission))
    return -1;
  if(!sprintf(res, format, value))
    return 0;
#endif
  if(value < 1.0)
    while(res[i]&&res[i]=='0')
      ++i; /* skip any leading zeroes */
  for(;res[i];++i,++j)
    buffer[j]=res[i]; /* copy rest of value */
  /* nice NULL termination */
  buffer[j]='\0';
  return 1;
}
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
removing leading and trailing whitespace from a string xytor Standard C, C++ 3 09-14-2005 11:11 AM


All times are GMT -8. The time now is 04:00 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting