Results 1 to 6 of 6

Thread: Problem of Decimal point in C

  1. #1
    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. #2
    Join Date
    Nov 2005
    Posts
    1,323

    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. #3
    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. #4
    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. #5
    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. #6
    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.

Similar Threads

  1. Replies: 3
    Last Post: 21-02-2012, 12:18 PM
  2. How to remove the decimal point for cell in Microsoft Excel
    By Kungfu Pandey in forum MS Office Support
    Replies: 1
    Last Post: 14-01-2012, 05:20 PM
  3. Replies: 6
    Last Post: 12-07-2011, 11:14 AM
  4. Excel Issue for decimal Point
    By Bassus in forum Windows Software
    Replies: 3
    Last Post: 15-01-2009, 09:54 AM
  5. Wireless Access Point Router problem in Windows
    By Aatbodh in forum Networking & Security
    Replies: 3
    Last Post: 22-11-2008, 04:39 PM

Posting Permissions

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