www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - converting numbers to strings and back

reply "dominik" <aha aha.com> writes:
I was trying, for fun and learning, to do some PHP style trash programming. 
Basically, I wanted to convert float to string, and string back to float. I 
did make it work, but not as documentation said I should. Look at the 
commented out line in example I made.

I'm trying to learn D (I'm coming from HEAVY C and PHP background), so I 
occasionally post questions which might seem retarded. I'm sorry, and thank 
you for your patience and help :)

Documentation for D is still pretty sparse on the interwebtubes - luckly 
this newsgroup is pretty active and helpfull. Is there a place where D 
documentation is centralized? Like php.net for PHP for example - which I 
find the most usefull documentation formats for any language (including 
standard library) I've been working with. I have found several places on 
net, but none seem to be active and/or full of information. I'd like to 
contribute as I'm learning, so others can find more information easily.

--------------------------------------------
import std.stdio;
import std.string;
import std.conv;

void main(char[][] args) {

 float fFloat = 214.4;
 float fBack = 0;

 char[] cTest = std.string.toString(fFloat);
 writefln(cTest);

 // fBack = to!(float)(cTest); // this does not work! Why?
 fBack = toFloat(cTest);
 // This does work, but docs say it will be deprecated
 // because to!(T) supercedes it.
 writefln(fBack);

}
-------------------------------------------- 
Oct 05 2007
next sibling parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
dominik wrote:
 I was trying, for fun and learning, to do some PHP style trash programming. 
 Basically, I wanted to convert float to string, and string back to float. I 
 did make it work, but not as documentation said I should. Look at the 
 commented out line in example I made.
 
 I'm trying to learn D (I'm coming from HEAVY C and PHP background), so I 
 occasionally post questions which might seem retarded. I'm sorry, and thank 
 you for your patience and help :)
 
 Documentation for D is still pretty sparse on the interwebtubes - luckly 
 this newsgroup is pretty active and helpfull. Is there a place where D 
 documentation is centralized? Like php.net for PHP for example - which I 
 find the most usefull documentation formats for any language (including 
 standard library) I've been working with. I have found several places on 
 net, but none seem to be active and/or full of information. I'd like to 
 contribute as I'm learning, so others can find more information easily.
 
 --------------------------------------------
 import std.stdio;
 import std.string;
 import std.conv;
 
 void main(char[][] args) {
 
  float fFloat = 214.4;
  float fBack = 0;
 
  char[] cTest = std.string.toString(fFloat);
  writefln(cTest);
 
  // fBack = to!(float)(cTest); // this does not work! Why?
  fBack = toFloat(cTest);
  // This does work, but docs say it will be deprecated
  // because to!(T) supercedes it.
  writefln(fBack);
 
 }
 -------------------------------------------- 

Using my tinfoil telepathy hat, I suspect you are... using a 1.x series compiler, which does not have the std.conv.to!(T) template. You'll need to either use D 2.0 for that, or just use toFloat. If you *are* using a 2.x series compiler, then we've got a problem :) -- Daniel
Oct 05 2007
parent reply "dominik" <aha aha.com> writes:
"Daniel Keep" <daniel.keep.lists gmail.com> wrote in message 
news:fe736n$27ol$1 digitalmars.com...
 Using my tinfoil telepathy hat, I suspect you are... using a 1.x series
 compiler, which does not have the std.conv.to!(T) template.  You'll need
 to either use D 2.0 for that, or just use toFloat.

ok I apparently was using 1.x compiler (I've downloaded what was linked from main page). Now when I have downloaded newest version (2.005) I get the following error with both methods (toFloat and to!): test.d(10): Error: cannot implicitly convert expression (toString(fFloat)) of ty pe const(char)[] to char[] same code, nothing changed, same error on both method of conversion.
Oct 06 2007
parent reply "dominik" <aha aha.com> writes:
ok after several more simple trials with 2.005 I still get this:

test.d(11): template instance identifier 'to' is not defined

so, do we have a problem here or am I missing something (like a module or 
something)? 
Oct 06 2007
parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
dominik wrote:
 "Daniel Keep" <daniel.keep.lists gmail.com> wrote in message
 news:fe736n$27ol$1 digitalmars.com...
 Using my tinfoil telepathy hat, I suspect you are... using a 1.x series
 compiler, which does not have the std.conv.to!(T) template.  You'll need
 to either use D 2.0 for that, or just use toFloat.

ok I apparently was using 1.x compiler (I've downloaded what was

 main page).
 Now when I have downloaded newest version (2.005) I get the following

 with both methods (toFloat and to!):

 test.d(10): Error: cannot implicitly convert expression

 of ty
 pe const(char)[] to char[]

 same code, nothing changed, same error on both method of conversion.

That's because you're trying to assign a constant type into a mutable type. Which is what the compiler is telling you. Either use 'auto' for the storage class, or explicit assign it to a 'string' (or const(char)[] if you really feel like it). dominik wrote:
 ok after several more simple trials with 2.005 I still get this:
 
 test.d(11): template instance identifier 'to' is not defined
 
 so, do we have a problem here or am I missing something (like a module or 
 something)? 

Well, line 11 was "writefln(cTest);", so I assume you've changed something, so I can't really help since I don't know what you've got any more. -- Daniel
Oct 06 2007
parent reply "dominik" <aha aha.com> writes:
"Daniel Keep" <daniel.keep.lists gmail.com> wrote in message 
news:fe8dbp$v10$1 digitalmars.com...
 That's because you're trying to assign a constant type into a mutable
 type.  Which is what the compiler is telling you.  Either use 'auto' for
 the storage class, or explicit assign it to a 'string' (or const(char)[]
 if you really feel like it).

thanks, auto did it - and I think I like auto from now on.
 Well, line 11 was "writefln(cTest);", so I assume you've changed
 something, so I can't really help since I don't know what you've got any
 more.

here is the deal - 2.005 ----------------------------- import std.stdio; import std.string; import std.conv; void main(char[][] args) { float fFloat = 214.4; float fBack = 0; float fTo = 0; auto cTest = std.string.toString(fFloat); // Works! writefln(cTest); fBack = toFloat(cTest); // Works too! writefln(fBack); fTo = to!(float)(cTest); // this does not work! to undefined } -----------------------------
Oct 06 2007
parent reply Derek Parnell <derek psych.ward> writes:
On Sat, 6 Oct 2007 18:43:21 +0200, dominik wrote:

 "Daniel Keep" <daniel.keep.lists gmail.com> wrote in message 
 news:fe8dbp$v10$1 digitalmars.com...
 That's because you're trying to assign a constant type into a mutable
 type.  Which is what the compiler is telling you.  Either use 'auto' for
 the storage class, or explicit assign it to a 'string' (or const(char)[]
 if you really feel like it).

thanks, auto did it - and I think I like auto from now on.
 Well, line 11 was "writefln(cTest);", so I assume you've changed
 something, so I can't really help since I don't know what you've got any
 more.

here is the deal - 2.005 ----------------------------- import std.stdio; import std.string; import std.conv; void main(char[][] args) { float fFloat = 214.4; float fBack = 0; float fTo = 0; auto cTest = std.string.toString(fFloat); // Works! writefln(cTest); fBack = toFloat(cTest); // Works too! writefln(fBack); fTo = to!(float)(cTest); // this does not work! to undefined } -----------------------------

I'm not having any problems with this (2.005). I tried you original code (changing 'char[] cTest' to 'string cTest') and it worked fine. I tried the code above without changing anything and that too worked fine. This makes me think that you have got an environment issue. Could it be that the import search path is still pointing to DMD v1? -- Derek Parnell Melbourne, Australia skype: derek.j.parnell
Oct 06 2007
parent "dominik" <aha aha.com> writes:
"Derek Parnell" <derek psych.ward> wrote in message 
news:1016m2g51r7al$.1qhvpehp6oa7y$.dlg 40tude.net...
 I tried the code above without changing anything and that too worked fine.
 This makes me think that you have got an environment issue. Could it be
 that the import search path is still pointing to DMD v1?

apparently I was stupid. I have downloaded 2.002 instead of 2.005, now everything works. d'uh :) thanks
Oct 07 2007
prev sibling parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
dominik wrote:
 I was trying, for fun and learning, to do some PHP style trash programming. 
 Basically, I wanted to convert float to string, and string back to float. I 
 did make it work, but not as documentation said I should. Look at the 
 commented out line in example I made.
 
 I'm trying to learn D (I'm coming from HEAVY C and PHP background), so I 
 occasionally post questions which might seem retarded. I'm sorry, and thank 
 you for your patience and help :)
 
 Documentation for D is still pretty sparse on the interwebtubes - luckly 
 this newsgroup is pretty active and helpfull. Is there a place where D 
 documentation is centralized? Like php.net for PHP for example - which I 
 find the most usefull documentation formats for any language (including 
 standard library) I've been working with. I have found several places on 
 net, but none seem to be active and/or full of information. I'd like to 
 contribute as I'm learning, so others can find more information easily.

Each page of the D documentation has a "Comments" button in the upper left corner. You can put any observations you make about the documentation itself there. For instance often things are not located where you think they are -- opApply is not documented on the operator overloading page for instance. In that case put a link from the comments page of where you thought it would be to the page where the info is actually found. Also little examples that clarify what the docs say are good to put there. As for writing full fledged standalone tutorials, I think there's a place for those on dsource. http://www.dsource.org/projects/tutorials/wiki. It's a wiki you you can add your own there.
 
 --------------------------------------------
 import std.stdio;
 import std.string;
 import std.conv;
 
 void main(char[][] args) {
 
  float fFloat = 214.4;
  float fBack = 0;
 
  char[] cTest = std.string.toString(fFloat);
  writefln(cTest);
 
  // fBack = to!(float)(cTest); // this does not work! Why?
  fBack = toFloat(cTest);
  // This does work, but docs say it will be deprecated
  // because to!(T) supercedes it.
  writefln(fBack);

That's 2.0 docs you're reading there. Confusing because it's the defaut URL and the only indication it's 2.0 is some small text in upper right corner of the page. For 1.0 you need to use toFloat. --bb
Oct 05 2007
parent "dominik" <aha aha.com> writes:
"Bill Baxter" <dnewsgroup billbaxter.com> wrote in message 
news:fe73ml$28is$1 digitalmars.com...
 Each page of the D documentation has a "Comments" button in the upper left 
 corner.

ah, I didn't notice them - thx
Oct 06 2007