We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › split float-numbers
Page Index Toggle Pages: 1
split float-numbers (Read 1192 times)
split float-numbers
Mar 31st, 2007, 1:40am
 
Hi folks,

i try since a couple of hours to cut a float-value inte 2 parts ...

I have the value '-0.123' and need only the part after the point '123'.

But split seems to work only on strings but not at floats ... maybe anyone can help.

Thanks in advance!

Best regards, stderr
Re: split float-numbers
Reply #1 - Mar 31st, 2007, 1:48am
 
Code:

float f = -0.123;
String s = ""+f; // convert float to string
String[] sA = split(s,"."); // split the string
int i = Integer.parseInt(sA[1]); // convert string to int
println(sA[1]+" "+i);

Re: split float-numbers
Reply #2 - Mar 31st, 2007, 10:21am
 
It works. Excactly that was i wanted.

Thanks Andreas!
Re: split float-numbers
Reply #3 - Apr 1st, 2007, 3:38pm
 
using processing methods:

Code:

float f = -0.123;
String s = str(f);
String[] sp = split(s, '.');
int[] pieces = int(sp);


or very compact:

Code:

float f = -0.123;
int[] pieces = int(split(str(f), '.'));


note, however, that in this case, you'll lose the negative because after the split, -0 is 0.
Re: split float-numbers
Reply #4 - Apr 1st, 2007, 5:20pm
 
Thanks fry,

but now i use another methode to split/convert ...

nf() ... see code below

Code:
void setup() { 
size(300, 300);
background(64);
noLoop();
}

void draw() {
stroke(128);
noFill();
arc(150,150,250,250,0,360);
arc(150,150,200,200,0,360);
arc(150,150,150,150,0,360);
arc(150,150,100,100,0,360);
arc(150,150,50,50,0,360);
stroke(255);
line(150,25,150,275);
line(25,150,275,150);

String XPOSF1 = "-0.050";
String YPOSF1 = "+0.050";

float xpos = float(XPOSF1);
float ypos = float(YPOSF1);

if ( xpos < 0 && ypos < 0) { // PosX im Minus und PosY im Minus
String posx_string = nf(xpos,1,3);
String posy_string = nf(ypos,1,3);
int posx_string_splitted[] = int(split(posx_string, ','));
int posy_string_splitted[] = int(split(posy_string, ','));
int coordinate_x = 150-posx_string_splitted[1];
int coordinate_y = 150+posy_string_splitted[1];
draw_point(coordinate_x, coordinate_y);
}

if (xpos > 0 && ypos < 0) { // PosX im Plus und PosY im Minus
String posx_string = nf(xpos,1,3);
String posy_string = nf(ypos,1,3);
int posx_string_splitted[] = int(split(posx_string, ','));
int posy_string_splitted[] = int(split(posy_string, ','));
int coordinate_x = 150+posx_string_splitted[1];
int coordinate_y = 150+posy_string_splitted[1];
draw_point(coordinate_x, coordinate_y);
}

if ( xpos > 0 && ypos > 0) { // PosX im Plus und YPos im Plus
String posx_string = nf(xpos,1,3);
String posy_string = nf(ypos,1,3);
int posx_string_splitted[] = int(split(posx_string, ','));
int posy_string_splitted[] = int(split(posy_string, ','));
int coordinate_x = 150+posx_string_splitted[1];
int coordinate_y = 150-posy_string_splitted[1];
draw_point(coordinate_x, coordinate_y);
}

if (xpos < 0 && ypos > 0) { // PosX im Minus und YPos im Plus
String posx_string = nf(xpos,1,3);
String posy_string = nf(ypos,1,3);
int posx_string_splitted[] = int(split(posx_string, ','));
int posy_string_splitted[] = int(split(posy_string, ','));
int coordinate_x = 150-posx_string_splitted[1];
int coordinate_y = 150-posy_string_splitted[1];
draw_point(coordinate_x, coordinate_y);
}
}

void draw_point(int xloc, int yloc)
{
stroke(255,0,0);
fill(255,0,0);
arc(xloc, yloc,7,7,0,360);
}
Page Index Toggle Pages: 1