convert float string to string

I'm communicating with a microcontroller using the serial port using a demo I found and it works great. The issue is I do not want a floating point number to go to the microcontroller. Right now it does, using the code below.

What I would like to do is drop the decimal (or round off) "subtext[counter]" before sending via myPort.write. I have tried converting it to an int, but then myport.write doesn't like it. If there is a way to send an INT via myPort.write that would also be a good solution.

if(counter<subtext.length){

pan = Float.valueOf(subtext[counter]);

pan = (pan + 0.01) * 11.2;

(subtext[counter]) = Float.toString(pan);

myPort.write(subtext[counter]);

Tagged:

Answers

  • nf expects ints, any way to do this with a string?

  • Can't you just use your float pan variable w/ nf()?

  • Use the round function to round the float to the nearest integer.

  • Yeah I've tried nf() and round. I'm using a serial monitor and I still see a decimal being transmitted.

    I think I can work around this if I could send an int vs string via serial.

    Is there a way to use myPort.write to send an int?

  • edited October 2016

    How about

    (subtext[counter]) = Integer.toString(round(pan));

  • Thanks. That helped, the output lost the decimal. But, now it's not doing the other math I have in there. Can you check this for me?

    float pan = Integer.parseInt(subtext[counter]);

    pan = (pan + 0.01) * 11.2;

    (subtext[counter]) = Integer.toString(round(pan));

    myPort.write(subtext[counter]);

    What's the code markdown here anyway?

    Thanks again.

  • edited October 2016 Answer ✓

    From the nf() reference:

    nf() is used to add zeros to the left and/or right of a number. This is typically for aligning a list of numbers. To remove digits from a floating-point number, use the int(), ceil(), floor(), or round() functions.

    If you only want to remove some of the floating point digits then it gets a bit more complex. But in your case, you want just an int.

    The question is whether you want low/low results:

    5.1 = 5
    5.9 = 5
    

    or low/high results:

    5.1 = 5
    5.9 = 6
    
    • low/low results: use int(), (int), or floor()
    • low/high results: use round()

    You can also use str() to convert any result to string.

    Examples:

    /**
     * Round Float to Int
     * 2016-10-24 Processing 3.2.1
     * https:// forum.processing.org/two/discussion/18679/convert-float-string-to-string
     **/
    
    float f5_1 = 5.123;
    float f5_9 = 5.987;
    
    // round() function
    println ( f5_1, "round()", round(f5_1) ); // 5.1 = 5
    println ( f5_9, "round()", round(f5_9) ); // 5.9 = 6
    
    // int() function
    println ( f5_1, "int()  ", int(f5_1) );   // 5.1 = 5
    println ( f5_9, "int()  ", int(f5_9) );   // 5.9 = 5
    
    // (int) cast
    println ( f5_1, "(int)  ", (int)f5_1 );   // 5.1 = 5
    println ( f5_9, "(int)  ", (int)f5_9 );   // 5.9 = 5
    
    // floor() function
    println ( f5_9, "floor()", floor(f5_1) ); // 5.1 = 5
    println ( f5_9, "floor()", floor(f5_9) ); // 5.9 = 5
    
    // ceil() function
    println ( f5_1, "ceil() ", ceil(f5_1) );  // 5.1 = 6
    println ( f5_9, "ceil() ", ceil(f5_9) );  // 5.9 = 6
    
    // ...convert rounded float to string
    println ( f5_9, "str(round())", str(round(f5_9)) ); // 5.9 = "6"
    

    Output:

    5.123 round() 5
    5.987 round() 6
    5.123 int()   5
    5.987 int()   5
    5.123 (int)   5
    5.987 (int)   5
    5.987 floor() 5
    5.987 floor() 5
    5.123 ceil()  6
    5.987 ceil()  6
    5.987 str(round()) 6
    
  • edited October 2016

    But, now it's not doing the other math

    Line 2 will take the value in pan add 0.01 and multiply by 11.2 and then store the result in pan overwritng the previous value.

    Line 3 rounds the value to the nearest integer and converts it to a String leaving pan unchanged

    What other math???

    Can you check this for me?

    No I don't use the serial library perhaps someone else can help you.

    Code markdown read the sticky post Format Text and Code

  • Thanks for all the help guys. We're up and running.

Sign In or Register to comment.