1

I am trying to convert a floating point number to string. I know you can do it using ostringstream & sprintf etc. but in the project I am working I am trying to do it using my own functions only (I am creating my own string class without using any outside functions). I don't want a perfect representation e.g. I don't mind it if this happens with large or small number: 1.0420753e+4 like it does with the standard stringstream.

I know how floating point numbers work (e.g. sign, exponent, mantissa) and how they are represented in a different way from what they are displayed as (that is why its difficult). I know this is possible because the std c++ library can do it - I just don't know how to do it myself.

EDIT: I have created my own integer version of this (converts int to my own CString class).

PersonWithName
  • 848
  • 2
  • 13
  • 19

4 Answers4

4

First, do not do this yourself. iOS has standard C++ features for formatting floating-point objects, and I expect Android does too.

Second, do not do this yourself. It is hard to do without rounding errors. The techniques for doing it are already known and published, and you should use good references rather than the algorithms you will generally find on Stack Overflow. The classic paper for this is Correctly Rounded Binary-Decimal and Decimal-Binary Conversions by David M. Gay, and here is code from David Gay.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
2

Simple method: Divide by 10 until the value is ≤ 1. This gives you the number of decimals after which you should print the .. Multiply the original number by 10 for each digit you want after the ., and round. Stringify the resulting integer, and insert the ..

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
MSalters
  • 173,980
  • 10
  • 155
  • 350
  • +1 that's cute, and probably exact if the floating point radix is 10. – Bathsheba Jun 28 '13 at 11:25
  • Oh, obviously. I thought the question was OK with that. – MSalters Jun 28 '13 at 13:35
  • I intended to vote against another answer, not this one. Stack Overflow has some bug where it applies to the vote to the wrong answer if answers are added/delete while you are viewing a page. But my answer is locked in now. I am going to make a small edit so that I can remove the vote. – Eric Postpischil Jun 28 '13 at 13:42
  • @MSalters: The question says the OP does not want a perfect representation, but the example given for that shows they mean that exponential notations are acceptable, not that incorrect results are acceptable. – Eric Postpischil Jun 28 '13 at 13:53
0

Uhm, if you really want to reinvent your own square wheel, then probably the easiest way is to write converter from float to int(you said you know how bit pattern works), or maybe even 2 ints - one for fractional part, other for the rest, then print them REUSING code that already exists

aryan
  • 621
  • 3
  • 10
  • The reason I am trying to write my own code is because I want to port my project to iOS and android and I want as less dependencies as I can (is the standard C++ library avaliable on mobile platforms?) – PersonWithName Jun 28 '13 at 11:49
  • You can definitely assume basic C functions are available, so sprintf'ing is your best bet. Basic stl(ie. stringstream) also should be there, but you're better of checking. But definitely don't write such low level code on your own! And that's the question you should ask yourself - is feature X available on mobile platforms. You can definitely google your way from there. – aryan Jun 28 '13 at 12:09
-1

Use ostringstream -:

double d = 2.7818; 
std::ostringstream ss;
ss << d;
std::cout << ss.str() << std::endl;
suspectus
  • 16,548
  • 8
  • 49
  • 57
  • "I know you can do it using ostringstream & sprintf etc. but in the project I am working I am trying to do it using my own functions only". Why suggesting immediately `ostringstream` when it's exactly about *not* using it... ? – JBL Jun 28 '13 at 11:08