Thread: Outputted as same as inputted

  1. #1
    Registered User main()'s Avatar
    Join Date
    Apr 2010
    Location
    Malaysia
    Posts
    14

    Question Outputted as same as inputted

    Hi,

    I kept it up too late last night trying to find an answer for my question which is:

    Is it possible for a float number to be displayed as it was entered?

    I mean if I enter 2.23, the output should be 2.23, and if I enter 2.123, the output should be 2.123, bothing entering using the same program, and no rounding-up rules, just showing the output the same way as the input.

    Thank you

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If you know how the input is formatted it is possible. You just print it out with printf in the same format you read it in.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Enter/process/output it as a string.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    Actual value you read are not get changed in any way, if you up to the range limits of course. What may change is how it is displayed.

    So, your 2.23 would look differently when used with different format strings.
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Important to understand the limitations in floating point. The only numbers that can be represented exactly are inverse power of two fractions: 1/2 (0.5), 1/4(0.25), 1/8(0.125), etc. Not included: 0.1

    Code:
    #include <stdio.h>
    
    int main() {
            float i;
            for (i=0.0f; i<20; i+=0.1f) {
                    printf("%f\n",i);
            }
            return 0;
    }
    The output is not what you expect.

    This is not to say you cannot get what you want, just you need to understand the problem first.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User main()'s Avatar
    Join Date
    Apr 2010
    Location
    Malaysia
    Posts
    14
    Ok, this is for more explaining:

    int main()
    {
    float num1, num2, add,

    printf("Input two numbers to add");
    scanf("%f%f", &num1, &num2);

    add = num1 + num2;

    printf("result of adding %f and %f is %f", num1, num2, add);

    return 0;

    }


    when I run the above program, and input for example 2.23 for num1 and 2.43 for num2, the output is like this:

    result of adding 2.230000 and 2.430000 is 4.66000

    I want the output to be the same as the input , input 2.23 output 2.23 not 2.230000, as well as any float number like 2.123 to be output like 2.123 not 2.123000



    HOW?

    and Why is the rounding-up six decimal points?

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's not rounding, it has a default precision. You can specify a fixed precision, but if you want it to be the exact way it was entered, you're best off with strings.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Use %.2f if you want 2 decimal places. To different numbers of decimal places depending on the input, you'll have to figure out a way of determining how many significant decimal places your number has between the reading in and the outputting.

    On the other hand, using strings makes it all quite simple.

  9. #9
    Registered User main()'s Avatar
    Join Date
    Apr 2010
    Location
    Malaysia
    Posts
    14

    Question

    Quote Originally Posted by quzah View Post
    It's not rounding, it has a default precision. You can specify a fixed precision, but if you want it to be the exact way it was entered, you're best off with strings.


    Quzah.

    Can you please explain more? example would be better if possible.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Explain which part, I said more than one thing there. There's a FAQ on reading input as a string.

    Cprogramming.com FAQ > Get a line of text from the user/keyboard (C)
    Cprogramming.com FAQ > How do I get a number from the user (C)


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User main()'s Avatar
    Join Date
    Apr 2010
    Location
    Malaysia
    Posts
    14
    Quote Originally Posted by KBriggs View Post
    Use %.2f if you want 2 decimal places. To different numbers of decimal places depending on the input, you'll have to figure out a way of determining how many significant decimal places your number has between the reading in and the outputting.

    On the other hand, using strings makes it all quite simple.

    How to use string for reading in floats? example please

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by KBriggs View Post
    Use %.2f if you want 2 decimal places.
    This will format the output. However, it is not dynamic, meaning 3.666 will appear as 3.67.

    As Quzah says, you will have to store the data as a string, and NOT a float, if you want that.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I suppose we could just use %g. Of course, if you enter "1.230", it won't print "1.230", it will print "1.23". So again, strings.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Here:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h> // needed for strtof
    
    int main(int argc, const char *argv[]) {
    	float n = 1.234;
    	char str[32], *p;
    
    	if (argc > 1) n = strtof(argv[1],NULL);
    
    	sprintf(str, "%f", n);
    	p = &str[strlen(str)-1];
    	while (*p == '0') *(p--) = '\0';
    
    	printf("%s\n", str);
    
    	return 0;
    }
    If you are using gcc strtof() requires -std=c99. This contains a potentially uncontrollable overflow though since sprintf can't limit the input length with %f.
    Last edited by MK27; 05-12-2010 at 03:01 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  15. #15
    Registered User main()'s Avatar
    Join Date
    Apr 2010
    Location
    Malaysia
    Posts
    14
    Thank you all guys so much for your very helpful responses, I solved the problem using strings

    This forum is the best ever

    Thank you again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. Replies: 6
    Last Post: 04-21-2006, 08:49 PM
  3. Inputted values into an array
    By Panther in forum C Programming
    Replies: 6
    Last Post: 04-11-2003, 10:15 AM
  4. Replies: 4
    Last Post: 03-25-2003, 01:23 AM
  5. I need help with saving inputted text to a file.
    By civix in forum C++ Programming
    Replies: 18
    Last Post: 08-04-2002, 12:36 PM