NO

Author Topic: convert double to string to double  (Read 3220 times)

magic

  • Guest
convert double to string to double
« on: September 22, 2008, 10:27:56 PM »
Is there a preset library to
convert from double to string and
convert from string to double

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: convert double to string to double
« Reply #1 on: September 23, 2008, 09:09:58 AM »
You would use sprintf to convert from a number to a string and any of the ato... or strto... functions to convert from string to number.
---
Stefan

Proud member of the UltraDefrag Development Team

magic

  • Guest
Re: convert double to string to double
« Reply #2 on: October 24, 2008, 06:10:59 AM »
could you give me sample how to  use sprintf(). The documentation is quite difficult for me to understand without sample.
Say, I want to convert a double number into a string.:
Code: [Select]
        double myDOUBLE=123.456;
char* mySTRING=GlobalAlloc(GMEM_FIXED,64);     //alocate string in memory
_strnset(mySTRING,0,1);                                   //initilize mySTRINGto ""
int sprintf(testing,myDOUBLE%???);                     //I can't really understand how to format it


Offline Robert

  • Member
  • *
  • Posts: 245
Re: convert double to string to double
« Reply #3 on: October 24, 2008, 06:56:45 AM »
Here are two examples

 Example 1
Code: [Select]

#include <windows.h>
#include <stdio.h>

int main(void) {
  double myDOUBLE=123.456;
  char* mySTRING=GlobalAlloc(GMEM_FIXED,64);     //allocate string in memory
  _strnset(mySTRING,0,1);                        //initilize mySTRINGto ""
  int i;
 
  i = sprintf(mySTRING, "%G", myDOUBLE);
  printf("123.456 as a string is %s\n", mySTRING);
  printf("sprintf returns: %d\n\n", i);

  return 0;
}


 Example 2

Code: [Select]

#include <stdio.h>

int main(void) {
  double dub1 = 123.456;
  char str1[10];
  int i;
 
  i = sprintf(str1, "%G", dub1);
  printf("123.456 as a string is %s\n", str1);
  printf("sprintf returns: %d\n\n", i);

  return 0;
}


I hope this helps.

Robert Wishlaw