question about mixing numbers and strings without using d2s() or parseFloat() first

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

question about mixing numbers and strings without using d2s() or parseFloat() first

Cammer, Michael-3
Would you please help me with a question about strings and numbers with operators?


I expected the following n= and n0= to give errors:


nstring = "" + "6.7635";
n = nstring + 17;
n0 = 17 + nstring;


However,

print(n);
print(n0);
yielded
6.763517
23.7635

Is this new in ImageJ that numbers and strings may be converted in expressions based on the type of the first element in an equation or have I simply not noticed it before?


Thank you!


Michael Cammer, Sr Research Scientist, DART Microscopy Laboratory

NYU Langone Health, 540 First Avenue, SK2 Microscopy Suite, New York, NY  10016

[hidden email]<mailto:[hidden email]>  http://nyulmc.org/micros  http://microscopynotes.com/

Voice direct only, no text or messages:  1-914-309-3270 and 1-646-501-0567


--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: question about mixing numbers and strings without using d2s() or parseFloat() first

CARL Philippe (LBP)
Dear Michael,
You may probably as well get a way sharper answer either from Michael (Schmid) or Wayne, but what you are describing is something that I'm used to see (and use).
You can even push it further and do this in java with the IJ.log method where you can write:
IJ.log("" + val);
val being an int without having to use any Integer.toString(val) or String.valueOf(val) method for the conversion.
Nevertheless for a string to int or flat conversion in macro language I still prefer to use the parseInt(string) or parseFloat(string) methods is order to be sure to not make any mistakes.
Have a nice end of week-end and take care!
Philippe

Philippe CARL
Laboratoire de Bioimagerie et Pathologies
UMR 7021 CNRS - Université de Strasbourg
Faculté de Pharmacie
74 route du Rhin
67401 ILLKIRCH
Tel : +33(0)3 68 85 42 89

----- Le 17 Mai 20, à 4:50, Cammer, Michael [hidden email] a écrit :

Would you please help me with a question about strings and numbers with operators?


I expected the following n= and n0= to give errors:


nstring = "" + "6.7635";
n = nstring + 17;
n0 = 17 + nstring;


However,

print(n);
print(n0);
yielded
6.763517
23.7635

Is this new in ImageJ that numbers and strings may be converted in expressions based on the type of the first element in an equation or have I simply not noticed it before?


Thank you!


Michael Cammer, Sr Research Scientist, DART Microscopy Laboratory

NYU Langone Health, 540 First Avenue, SK2 Microscopy Suite, New York, NY  10016

[hidden email]<mailto:[hidden email]>  http://nyulmc.org/micros  http://microscopynotes.com/

Voice direct only, no text or messages:  1-914-309-3270 and 1-646-501-0567


--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html