Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Problem of Decimal point in C

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 23-12-2009
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'?

Reply With Quote
  #2  
Old 23-12-2009
Member
 
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.
Reply With Quote
  #3  
Old 23-12-2009
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
Reply With Quote
  #4  
Old 23-12-2009
Member
 
Join Date: May 2008
Posts: 2,382
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 .
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #5  
Old 23-12-2009
Member
 
Join Date: Feb 2008
Posts: 1,845
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.
Reply With Quote
  #6  
Old 23-12-2009
Member
 
Join Date: Jan 2008
Posts: 1,515
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Problem of Decimal point in C"
Thread Thread Starter Forum Replies Last Post
Stucked beween Decimal point and common issue in Microsoft Excel Aza MS Office Support 3 21-02-2012 12:18 PM
How to remove the decimal point for cell in Microsoft Excel Kungfu Pandey MS Office Support 1 14-01-2012 05:20 PM
VLC 1.1.7/openSUSE 11.3 having Floating point exception problem Bryant Windows Software 6 12-07-2011 11:14 AM
Excel Issue for decimal Point Bassus Windows Software 3 15-01-2009 09:54 AM
Wireless Access Point Router problem in Windows Aatbodh Networking & Security 3 22-11-2008 04:39 PM


All times are GMT +5.5. The time now is 03:30 PM.