Re: Distinguish float and integer types from string

2019-03-11 Thread XavierAP via Digitalmars-d-learn
On Monday, 11 March 2019 at 15:03:39 UTC, XavierAP wrote: What compiler version are you using? I on the other hand was surprised that I needed the try-catch above, after having already checked isNumeric. The documentation claims that the conversion to int or long would truncate, but my

Re: Distinguish float and integer types from string

2019-03-11 Thread XavierAP via Digitalmars-d-learn
On Saturday, 9 March 2019 at 18:11:09 UTC, Jacob Shtokolov wrote: One of the task was to take a string from STDIN and detect its type. There were a few options: Float, Integer, string and "something else" (which, I think, doesn't have any sense under the scope of the task). Another

Re: Distinguish float and integer types from string

2019-03-11 Thread Johann Lermer via Digitalmars-d-learn
On Saturday, 9 March 2019 at 18:11:09 UTC, Jacob Shtokolov wrote: I tried to use std.conv.to and std.conv.parse, but found that they can't really do this. When I call `data.to!int`, the value of "123.45" will be converted to int! Are you sure? This here works for me: import std.stdio; import

Re: Distinguish float and integer types from string

2019-03-11 Thread Soulsbane via Digitalmars-d-learn
On Saturday, 9 March 2019 at 18:11:09 UTC, Jacob Shtokolov wrote: Hi, Recently, I was trying to solve some funny coding challenges (https://www.techgig.com). The questions were really simple, but I found it interesting because the website allows to use D. One of the task was to take a

Re: Distinguish float and integer types from string

2019-03-10 Thread spir via Digitalmars-d-learn
On 09/03/2019 19:11, Jacob Shtokolov via Digitalmars-d-learn wrote: The thing is that in PHP, for example, I would do The thing is php needs to be able to "lexify" raw input data at runtime, while in D this is done at compile-time. The ompiler has the lexer to do that. But I agree that, for

Re: Distinguish float and integer types from string

2019-03-09 Thread Adam D. Ruppe via Digitalmars-d-learn
On Saturday, 9 March 2019 at 18:11:09 UTC, Jacob Shtokolov wrote:w One of the task was to take a string from STDIN and detect its type. The way I'd do this is a very simple loop: enum Type { String, Float, Int } if(str.length && str[0] == '-') { str = str[1 .. $]; } Type type = str.length