Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   data type for decimal number (http://www.velocityreviews.com/forums/t592146-data-type-for-decimal-number.html)

neha_chhatre@yahoo.co.in 02-16-2008 06:40 PM

data type for decimal number
 
which is the best format specifier(data type) if i have to work with
decimal number.

also please tell me the syntax for truncating a decimal number

please reply as soon as possible

Richard Heathfield 02-16-2008 06:50 PM

Re: data type for decimal number
 
neha_chhatre@yahoo.co.in said:

> which is the best format specifier(data type) if i have to work with
> decimal number.


There is no such thing as a "decimal number". Numbers are numbers. But
presumably you mean that you wish to display a number using decimal
notation, and your reference to a format specifier suggests that you wish
to do this using printf.

If you have an int type, use %d or %i. For short int and long int, apply
the appropriate modifiers, as listed in your C book (in K&R2 they're on
page 244). For unsigned int types, use %u rather than %d or %i.

If you wish to display a floating point type, use %f.

> also please tell me the syntax for truncating a decimal number


This depends on what you mean by "truncate" and on what type of value you
wish to truncate (and particularly on whether you want the stored value to
be truncated, or merely the displayed value).

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

Malcolm McLean 02-16-2008 09:24 PM

Re: data type for decimal number
 

<neha_chhatre@yahoo.co.in> wrote in message news:
> which is the best format specifier(data type) if i have to work with
> decimal number.
>
> also please tell me the syntax for truncating a decimal number
>

%d, d is for decimal, is the standard printf format specifier for an int.
There are several modifiers you can pass to control the way the number
appears, but one thing you cannot do is to lose digits.

Look up your printf() documentationto get the details.

You might also want to look at the %f and %g formats for floating-point
numbers. These too are printed out in decimal, and this case non-significant
digits can be truncated.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


Robert W Hand 02-17-2008 08:17 PM

Re: data type for decimal number
 
On Sat, 16 Feb 2008 18:50:57 +0000, Richard Heathfield
<rjh@see.sig.invalid> wrote:

>There is no such thing as a "decimal number". Numbers are numbers. But
>presumably you mean that you wish to display a number using decimal
>notation, and your reference to a format specifier suggests that you wish
>to do this using printf.


A tiny point. The Standard does use the term "decimal number" in the
section on strftime(). Its meaning is exactly what you wrote about
display, but I would avoid writing that there is no such thing as ....
You know that there is always an exception. ;-)

Richard Heathfield 02-18-2008 01:43 AM

Re: data type for decimal number
 
Robert W Hand said:

> On Sat, 16 Feb 2008 18:50:57 +0000, Richard Heathfield
> <rjh@see.sig.invalid> wrote:
>
>>There is no such thing as a "decimal number". Numbers are numbers. But
>>presumably you mean that you wish to display a number using decimal
>>notation, and your reference to a format specifier suggests that you wish
>>to do this using printf.

>
> A tiny point. The Standard does use the term "decimal number" in the
> section on strftime(). Its meaning is exactly what you wrote about
> display, but I would avoid writing that there is no such thing as ....
> You know that there is always an exception. ;-)


The fact that the Standard uses the term "decimal number" does not mean
there is such a thing as a decimal number. It merely means that the people
who wrote that part of the Standard think there is such a thing as a
decimal number.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

osmium 02-18-2008 02:55 AM

Re: data type for decimal number
 
"Richard Heathfield" writes:

> Robert W Hand said:
>
>> On Sat, 16 Feb 2008 18:50:57 +0000, Richard Heathfield
>> <rjh@see.sig.invalid> wrote:
>>
>>>There is no such thing as a "decimal number". Numbers are numbers. But
>>>presumably you mean that you wish to display a number using decimal
>>>notation, and your reference to a format specifier suggests that you wish
>>>to do this using printf.

>>
>> A tiny point. The Standard does use the term "decimal number" in the
>> section on strftime(). Its meaning is exactly what you wrote about
>> display, but I would avoid writing that there is no such thing as ....
>> You know that there is always an exception. ;-)

>
> The fact that the Standard uses the term "decimal number" does not mean
> there is such a thing as a decimal number. It merely means that the people
> who wrote that part of the Standard think there is such a thing as a
> decimal number.


What's your point? The same background is behind every word in every
language. Bread is what someone thought bread was. You seem to delight in
bringing up the standard when it pleases you. But if it disagrees with your
belief system in some tiny way it becomes "just another document in the
wearisome pile of documents produced by humankind."



Kenny McCormack 02-18-2008 03:22 AM

Re: data type for decimal number
 
In article <ROCdnX1flqRUfCXanZ2dneKdnZydnZ2d@bt.com>,
Richard Heathfield <rjh@see.sig.invalid> wrote:
>Robert W Hand said:
>
>> On Sat, 16 Feb 2008 18:50:57 +0000, Richard Heathfield
>> <rjh@see.sig.invalid> wrote:
>>
>>>There is no such thing as a "decimal number". Numbers are numbers. But
>>>presumably you mean that you wish to display a number using decimal
>>>notation, and your reference to a format specifier suggests that you wish
>>>to do this using printf.

>>
>> A tiny point. The Standard does use the term "decimal number" in the
>> section on strftime(). Its meaning is exactly what you wrote about
>> display, but I would avoid writing that there is no such thing as ....
>> You know that there is always an exception. ;-)

>
>The fact that the Standard uses the term "decimal number" does not mean
>there is such a thing as a decimal number. It merely means that the people
>who wrote that part of the Standard think there is such a thing as a
>decimal number.


I will leave it to my esteemed colleague, Mr. Twink, to give this post
all the respect that it deserves.


Richard Heathfield 02-18-2008 03:26 AM

Re: data type for decimal number
 
osmium said:

> "Richard Heathfield" writes:
>
>> Robert W Hand said:
>>
>>> On Sat, 16 Feb 2008 18:50:57 +0000, Richard Heathfield
>>> <rjh@see.sig.invalid> wrote:
>>>
>>>>There is no such thing as a "decimal number". Numbers are numbers. But
>>>>presumably you mean that you wish to display a number using decimal
>>>>notation, and your reference to a format specifier suggests that you
>>>>wish to do this using printf.
>>>
>>> A tiny point. The Standard does use the term "decimal number" in the
>>> section on strftime(). Its meaning is exactly what you wrote about
>>> display, but I would avoid writing that there is no such thing as ....
>>> You know that there is always an exception. ;-)

>>
>> The fact that the Standard uses the term "decimal number" does not mean
>> there is such a thing as a decimal number. It merely means that the
>> people who wrote that part of the Standard think there is such a thing
>> as a decimal number.

>
> What's your point?


That there is no such thing as a decimal number. The fact that the Standard
claims otherwise is a flaw in the Standard.

> You seem to delight in bringing up the standard when it pleases you.


I bring up the Standard when it's relevant. What pleases me is the
opportunity to help people learn more about C. C is defined by the
Standard (although the failure of C99 makes that a rather shaky stance).
Citing the Standard on occasion is therefore inevitable. But delight? I
don't think so.

> But if it disagrees with
> your belief system in some tiny way it becomes "just another document in
> the wearisome pile of documents produced by humankind."


It's an important document, but it's not perfect, and we'd be fools to
think otherwise.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

Antoninus Twink 02-18-2008 08:43 AM

Re: data type for decimal number
 
On 18 Feb 2008 at 3:22, Kenny McCormack wrote:
> In article <ROCdnX1flqRUfCXanZ2dneKdnZydnZ2d@bt.com>,
> Richard Heathfield <rjh@see.sig.invalid> wrote:
>>Robert W Hand said:
>>
>>> On Sat, 16 Feb 2008 18:50:57 +0000, Richard Heathfield
>>> <rjh@see.sig.invalid> wrote:
>>>
>>>>There is no such thing as a "decimal number". Numbers are numbers. But
>>>>presumably you mean that you wish to display a number using decimal
>>>>notation, and your reference to a format specifier suggests that you wish
>>>>to do this using printf.
>>>
>>> A tiny point. The Standard does use the term "decimal number" in the
>>> section on strftime(). Its meaning is exactly what you wrote about
>>> display, but I would avoid writing that there is no such thing as ....
>>> You know that there is always an exception. ;-)

>>
>>The fact that the Standard uses the term "decimal number" does not mean
>>there is such a thing as a decimal number. It merely means that the people
>>who wrote that part of the Standard think there is such a thing as a
>>decimal number.

>
> I will leave it to my esteemed colleague, Mr. Twink, to give this post
> all the respect that it deserves.


It would be nice to say something funny and cutting, but Heathfield's
post is so far off into la-la land that it's beyond the power of parody
to match the absurdity of the post itself... Still, it must be fun in a
way to be Heathfield, completely unconstrained by reality.


Nick Keighley 02-18-2008 09:15 AM

Re: data type for decimal number
 
On 16 Feb, 18:40, neha_chha...@yahoo.co.in wrote:

> which is the best format specifier(data type) if i have to work with
> decimal number.


what is a "decimal number"? If you are working with integers
then use integers and display the result in decimal. If floating point
use double then display in decimal.

Or do you want some form of decimal floating point (I doubt it).


> also please tell me the syntax for truncating a decimal number


try the documentaion for printf()


> please reply as soon as possible


why?


--
Nick Keighley




All times are GMT. The time now is 01:08 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.