# Thread: Problem of Decimal point in C

1. Member
Join Date
Dec 2009
Posts
140

## Problem of Decimal point in C

hello, I am new C programmer and i am having trouble with the scanf function whenever i do scanf("%f",n1); and i try to enter some numbers like 12.34 . After that when i try to output those numbers using the printf 12.340000 . please suggest some steps so that in scanf the n1 should be 12.34 it should not contain any zero '0'?

2. Member
Join Date
Nov 2005
Posts
1,322

## Re: Problem of Decimal point in C

So just a very simple trouble this problem will be solved if you use %2f in the printf function. That means your code will become printf("%2f",n1). This function of the printf commands the C compiler to output the values only upto decimal places. that is only 12.34 will be the out put as required by you.

3. Member
Join Date
Dec 2009
Posts
140

## Re: Problem of Decimal point in C

I mean when i am calling printf("%2f",n1) it will get 12.34, since the value in num is still 12.340000 it does not help me i would be happy if anybody tell me how can i change the value of n1 to 12.34

4. Member
Join Date
May 2008
Posts
2,393

## Re: Problem of Decimal point in C

Try to use double instead of float or if you want to avoid the accumulation of errors that float and double cannot represent every real number rearrange the order of certain operations use decimal math library or move to some other programming language that has a decimal type. if you want to try some other way read a string from the user using the fgets(), then convert the input to what called a scaled integer value. Now, you'll have to occasionally convert to double or float for some operations and then back again .

5. Member
Join Date
Feb 2008
Posts
1,856

## Re: Problem of Decimal point in C

Can you just tell me how many discrete values 32 bits or 64 bits can represents. Then think is it possible that same number of bits can represent values a thousand times as large. The answer is that it lumps a lot of values together under oa single number.

6. Member
Join Date
Jan 2008
Posts
1,521

## Re: Problem of Decimal point in C

As far as i know the 12.34 value is same as 12.34 . Also i am very surprised to know %f would output trailing zeroes. which it avoids normally. Either the stored value is 12.340001 instead of 12.340000, Since by default %f always displays upto six decimal places only. And in your program if it displays such unusual . Then before giving you the solution you please specify me the type of compiler you are using. try to View the variable in your debugger to see all significant digits.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•