Thread: Double type

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    162

    Double type

    Hi

    Do you know about a way how to work with (double) types in text controls...

    iTotal is type (double), imagine it carries a value of 3.14159.

    Code:
    wsprintf(chTotal, "%d", (double)iTotal);
    SetDlgItemText(hwndDlg, nControl, chTotal);
    Using this code returns me "1413551940" even if i try

    Code:
    wsprintf(chTotal, "%d", (float)iTotal);
    SetDlgItemText(hwndDlg, nControl, chTotal);
    the same error ocures...

    Only works if I request (int) value but that is not with decimal place. I am quite confused why does the programme return this?

    Can you plase help with that somehow? THX

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Try %f instead of %d.
    Reading the manual to check on the relationship between conversion formats and parameter types also works.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    162
    Quote Originally Posted by Salem View Post
    Try %f instead of %d.
    Reading the manual to check on the relationship between conversion formats and parameter types also works.
    Yes I did check, but didnt find any sollution. The %f returns only character "f"

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What can I say, it looks like wsprintf() is another broken thing.

    Use sprintf() to format your double as a string, then use wsprintf("%s") with that result.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    162
    Quote Originally Posted by Salem View Post
    What can I say, it looks like wsprintf() is another broken thing.

    Use sprintf() to format your double as a string, then use wsprintf("%s") with that result.
    Sorry I dont really understand. I tried a few ways how might it work but with no success. Can you write a little piece of code to show how does the sprintf() really work with further cooperation with wsprintf()?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    ...
    char buf[100];
    wchar wbuf[100];
    ...
    sprintf(buf, "%f", someDouble);
    wsprintf(wbuf, "%s", buf);
    ...
    I haven't tried it, so it may not work...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    You could use a stringstream to convert the double into a string; I had a program that required getting and displaying doubles, and I used stringstreams to convert between strings and the doubles. Here's the link, just use a double instead of int.

    If you're not using C++ strings, you could try this, but I haven't tried it so I don't know if it would work with doubles.

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    162
    Quote Originally Posted by matsp View Post
    Code:
    ...
    char buf[100];
    wchar wbuf[100];
    ...
    sprintf(buf, "%f", someDouble);
    wsprintf(wbuf, "%s", buf);
    ...
    I haven't tried it, so it may not work...

    --
    Mats
    Ok thx now it works

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 09:13 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM