Thread: Replacing dot with comma

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    29

    Replacing dot with comma

    Hey.

    I have been wondering about this for some time now, and when I couldnt get to a solution myself, I tried with google.

    I know there is a function in string to check for the first character you want it to find, etc. And find how many of that character theres in the string. But how do I replace something in a long float value?

    like

    double 3.234

    I just want a few tips on how I could get started. That would be really awesome.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    You change float (and int) values by doing math on them. The decimal place is inherent in a floating point value, though, so you're not going to replace it as such.

    If you want to print out 3.234 as 3,234 there are a few approaches. First, you can look into localization - e.g. The GNU C Library - Locales and Internationalization. I think this is POSIX rather than standard C, so your compiler may or may not support it.

    The other approach, if you just need to do this for floating point values is to print them into a string using sprintf() and then use whatever approach is easiest to find and replace '.' with ',' (strchr() seems as good a suspect as any).

    I'd prefer the former approach if you're doing this for real code. If you're doing it for a school assignment your instructor probably expects the latter. Whether they'd be impressed with or consider it cheating if you use the C localization functions is up for grabs.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by lovelace View Post
    But how do I replace something in a long float value?

    like

    double 3.234
    Math or bit manipulation. You can't put a comma in there because it isn't a character type. Also, you cannot deal with a "single element" (such as '3' or '.') because they do not exist; it is not an array, it's just one value.

    "3.234" is a convenient decimal representation which your compiler recognizes. Consider:

    Code:
    char a[] = "123456789";
    int b = 123456789;
    int c = 3;
    In memory, the first one is 10 bytes long, 1 for each character/digit plus a null terminator. The second one is only four bytes long (or whatever the fixed size of an int is on your system). The third one is the exact same size as the second one because they are the same type, altho the value of b is > c.

    Numbers are actually stored using power of two binary*. So actually are characters, each one is a single 8-bit byte with 256 possible combinations. "Power of two" means the possible combinations increase with each bit (binary digit), so 32-bits (a normal int) have 4294967295 possible combinations. For signed integers, this is the range -2147483648 to 2147483647. Try this:

    Code:
    	unsigned int x = ~0;
    	printf("%u\n", x, n);
    ~ inverts the bits. 0 is no bits set, so inverted, it is all bits sets; therefore this will print "4294967295".

    * signed types use a slight variation called two's complement.
    Last edited by MK27; 11-17-2011 at 09:42 AM.
    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

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    29
    Ah cheers, ill have a look at it later, thanks for taking your time for answering I didnt know what to expect, but now I got someplace to start

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by lovelace View Post
    Ah cheers, ill have a look at it later, thanks for taking your time for answering I didnt know what to expect, but now I got someplace to start
    Are you trying to display the european money format such as 123,06?

    That's actually pretty easy to do...
    Use sprintf() to write the floating point value to a string.
    Use strchr() to find the dot
    Then change the value at the pointer to a coma...
    Code:
    char EuroStd[16];
    char * dot;
    
    sprintf(EuroStd,"%.2f", value);
    
    dot = strchr(EuroStd,'.');
    
    if (dot)
      *dot = ',';
    
    printf("%s",EuroStd);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comma operator
    By ssharish2005 in forum C Programming
    Replies: 2
    Last Post: 10-20-2010, 05:35 AM
  2. Number WITH appropriate comma's
    By Adak in forum C Programming
    Replies: 5
    Last Post: 06-16-2007, 11:31 PM
  3. Comma Operator
    By doraiashok in forum C Programming
    Replies: 2
    Last Post: 12-09-2003, 08:38 AM
  4. comma
    By hug in forum C# Programming
    Replies: 1
    Last Post: 12-14-2002, 10:48 AM
  5. Replace "." (dot-comma) with "," (regular-comma) for MS Excel
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-20-2002, 01:39 AM