Home » Extra libraries, Code snippets, applications etc. » U++ Esc Interpreter, Esc Macros and templates » Esc: how to convert from string to number (or integer)?
|
|
Re: Esc: how to convert from string to number (or integer)? [message #3057 is a reply to message #3056] |
Fri, 05 May 2006 13:49 |
|
fudadmin
Messages: 1321 Registered: November 2005 Location: Kaunas, Lithuania
|
Ultimate Contributor Administrator |
|
|
luzr wrote on Fri, 05 May 2006 12:33 |
fudadmin wrote on Fri, 05 May 2006 07:27 | Esc: how to convert from string to number (or integer)?
Thanks.
|
Well, either write your library function (see Esc/EscStdLib.cpp) or write it in Esc...
(of course, now this depends what you are currently playing with. If with Esc within TheIDE, later is perhaps the only option...)
Mirek
|
I'm trying to write a very clever template... in *.upt,
where the amount of some functions and their names depend on a value entered into custom id field...
[Updated on: Fri, 05 May 2006 13:49] Report message to a moderator
|
|
|
|
|
Re: Esc: how to convert from string to number (or integer)? [message #3064 is a reply to message #3059] |
Fri, 05 May 2006 18:16 |
|
fudadmin
Messages: 1321 Registered: November 2005 Location: Kaunas, Lithuania
|
Ultimate Contributor Administrator |
|
|
luzr wrote on Fri, 05 May 2006 13:37 |
fudadmin wrote on Fri, 05 May 2006 07:52 | If that not exists, then I'll try to write in EscStdLib... It might be more useful later?
|
It might be. I think, as long as there is to_string, there likely should be some sort of atoi (or to_int)?
Go on, do it, post here, I will add it to EscStdLib. (actually, it will be about 5 lines
Mirek
|
//in EscStdLib.cpp
//aris +8lines
void ESC_to_number(EscEscape& e)
{
if(e[0].IsArray()){
String s=e[0];
double d = ScanDouble(s);
e = e[0].ToNumber(d);
}
}
//...
void StdLib(ArrayMap<String, EscValue>& global)
{
Escape(global, "is_number(value)", ESC_is_number);
Escape(global, "is_array(value)", ESC_is_array);
Escape(global, "is_map(value)", ESC_is_map);
Escape(global, "is_void(value)", ESC_is_void);
Escape(global, "int(value)", ESC_int);
Escape(global, "to_string(value)", ESC_to_string);
//aris +1 line
Escape(global, "to_number(value)", ESC_to_number);
//...
//in Esc.h
double GetNumber() const { return IsNumber() ? number : 0; }
bool IsInt() const;
int GetInt() const;
//aris +1 line
double ToNumber(double n) { Free(); number=n; type = ESC_NUMBER; return n;}
10lines=2*5lines ...
I was lazy to go into the details and haven't tested it extensively. But it works for me...
Is it correct from your point of view?
[Updated on: Fri, 05 May 2006 18:16] Report message to a moderator
|
|
|
Re: Esc: how to convert from string to number (or integer)? [message #3065 is a reply to message #3064] |
Fri, 05 May 2006 18:24 |
|
mirek
Messages: 13986 Registered: November 2005
|
Ultimate Member |
|
|
fudadmin wrote on Fri, 05 May 2006 12:16 |
luzr wrote on Fri, 05 May 2006 13:37 |
fudadmin wrote on Fri, 05 May 2006 07:52 | If that not exists, then I'll try to write in EscStdLib... It might be more useful later?
|
It might be. I think, as long as there is to_string, there likely should be some sort of atoi (or to_int)?
Go on, do it, post here, I will add it to EscStdLib. (actually, it will be about 5 lines
Mirek
|
//in EscStdLib.cpp
//aris +8lines
void ESC_to_number(EscEscape& e)
{
if(e[0].IsArray()){
String s=e[0];
double d = ScanDouble(s);
e = e[0].ToNumber(d);
}
}
//...
void StdLib(ArrayMap<String, EscValue>& global)
{
Escape(global, "is_number(value)", ESC_is_number);
Escape(global, "is_array(value)", ESC_is_array);
Escape(global, "is_map(value)", ESC_is_map);
Escape(global, "is_void(value)", ESC_is_void);
Escape(global, "int(value)", ESC_int);
Escape(global, "to_string(value)", ESC_to_string);
//aris +1 line
Escape(global, "to_number(value)", ESC_to_number);
//...
//in Esc.h
double GetNumber() const { return IsNumber() ? number : 0; }
bool IsInt() const;
int GetInt() const;
//aris +1 line
double ToNumber(double n) { Free(); number=n; type = ESC_NUMBER; return n;}
10lines=2*5lines ...
I was lazy to go into the details and haven't tested it extensively. But it works for me...
Is it correct from your point of view?
|
Not quite optimal.... This is simpler and not adding weird methods to EscValue:
void ESC_to_number(EscEscape& e)
{
if(e[0].IsArray())
e = ScanDouble((String)e[0]);
}
(now in U++)
Mirek
|
|
|
|
Goto Forum:
Current Time: Mon Jun 17 08:07:15 CEST 2024
Total time taken to generate the page: 0.03262 seconds
|