I'm trying to convert a double w/ a string of characters w/o using sprintf and padding w/ 0's.. omg... driving me nutz..

what I have thus far..

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <deque>

using namespace std;

void d2s(double,char[], unsigned int);

int main()
{
	while (true) 
	{
		double num;
		unsigned int size;
		printf ("Input number: ");
		cin >> num;
		printf ("Input size of char array: ");
		cin >> size;
		
		char* s = new char[size];
		dtoa(num, s, size); 
		printf("printf %s\n",s);
		cout << " cout = " << s << endl;
		
	}
}

void d2s(double num, char s[], unsigned int width)
{
	--width; // allow room for \0.
	double decimal = abs(num) - abs(int(num));
	int integer = abs(int(num));
	deque<char> result;
	
	while (integer != 0)
	{
		result.push_front(integer % 10 + '0');
		integer /= 10;
	}
	
	if (num < 0)  // check for negative value
		result.push_front('-');
	
	if (decimal != 0)  // check for decimal value
		result.push_back('.');
	
	while (abs(decimal) != 0  && result.size() < width)
	{
		decimal *= 10;
		result.push_back(int(decimal) % 10 + '0');
		decimal = decimal - int(decimal);
	}

	while (result.size() < width)
	{
		result.push_front(' '); // pad with spaces.
	}

	for (int i=0; i<width; ++i)  // results into array s
	{
		s[i] = result[i];
	}
	s[width] = '\0';
}

Look at <stringstream> in the stl. You know how you can pass any standard numeric type to cout and it knows how to print it? Well, stringstream knows how to convert any standard numeric type to a string. Furthermore, stringstreams allow iomanip formatting, so you can pad numbers and other such manipulations.

this is for a C program.. not C++. I posted in here because I was gonna build in C then transform.. so I doubt I can use stringstream.. =/ ugh

this is for a C program.. not C++. I posted in here because I was gonna build in C then transform.. so I doubt I can use stringstream.. =/ ugh

You won't have much luck with this in C. Everything you've done so far is based on C++. C is a subset of C++ and is totally different from what you have done. These are all C++ constructs: <iostream>, <deque>, <cmath>, <cstdlib>, std::cin, std::cout.

The only things you've really done that aren't C++ specific are using printf() and abs().

You won't have much luck with this in C. Everything you've done so far is based on C++. C is a subset of C++ and is totally different from what you have done. These are all C++ constructs: <iostream>, <deque>, <cmath>, <cstdlib>, std::cin, std::cout.

The only things you've really done that aren't C++ specific are using printf() and abs().

I can swap cmath w/ math.h, iostream gets nulled out when all cin and cout's get removed and replaced w/ printf and scanf. cstdlib is native C and is merely forward compatible w/ C++. deque is C++ but I could easily right a function that would do the same.

this is for a C program.. not C++. I posted in here because I was gonna build in C then transform.. so I doubt I can use stringstream.. =/ ugh

If it's a C program, you need to post it in the C Forum. It doesn't matter what you want to do with it later.

I'm trying to convert a double w/ a string of characters w/o using sprintf and padding w/ 0's.. omg... driving me nutz..

what I have thus far..

and what does it do thus far. And what doesn't it do. Don't leave it to us to figure out what it does wrong. Tell us. Then we can look at the code understanding what we are looking for.

In C++ easy as 1,2,3 :

double toDouble(string str) //1 -- define function
{
  //2 -- use simple logic and some stream object
   stringstreamm convert; 
   convert << str;
   double val = 0.0;
   convert >> val;
  return val; //3 -- return value, assuming it was a success.
}

it's hard to comment on what it does and does not do b/c I realized I shouldn't be using the deque library.

So essentially I'm trying to take a double value... and into a character array. So obviously must end in /0. if array is larger than the spaces used to fill.. blanks must be entered on the left. The key point.. I can't use sprintf.. =/

if it was an integer it would be nice and easy %10 and toss in array etc.. but I have to deal w/ decimal points, negative value.

I think I need to come up w/ a new approach. Unfortunately I'm a C++ guy and this has to be native C.

Edited 6 Years Ago by DJPlayer: n/a

Assume you have num=103.45
Get rid of the decimal -- num*100, decimal=2
Now you have a slightly more complex version of integer.

Assume you have num=103.45
Get rid of the decimal -- num*100, decimal=2
Now you have a slightly more complex version of integer.

I believe I have to keep track of that decimal. If I converted every decimal to an integer I would lose the placeholder for that "." When inserting it to an array I would simply have 10345 w/ no clue where that "." should go.

storing in an array I'm assuming it should be |_|...|1|0|3|.|4|5|/0|
(the first couple slot of the array were misc spaces).

I believe I have to keep track of that decimal. If I converted every decimal to an integer I would lose the placeholder for that "." When inserting it to an array I would simply have 10345 w/ no clue where that "." should go.

storing in an array I'm assuming it should be |_|...|1|0|3|.|4|5|/0|
(the first couple slot of the array were misc spaces).

People need to think rather than react...

Let me expand what I posted:

Assume you have num=103.45
Get rid of the decimal -- num*100, decimal=2

Now assume you have num=221.5
Get rid of the decimal -- num*10, decimal=1

And this time assume you have num=341.236
Get rid of the decimal -- num*1000, decimal=3 decimal is a variable.

Out of curiosity, too, what are you going to do for really large/small values? Like...

#include <stdio.h>

int main(void) 
{
   double d = -123.456e-52;
   printf("d = %f = %g\n", d, d);
   d = -123.456e+52;
   printf("d = %f = %g\n", d, d);
   return 0;
}
This article has been dead for over six months. Start a new discussion instead.