NFC() doesn't behave as expected in processing.js

edited April 2014 in JavaScript Mode

Hi all,

Having trouble with the nfc function when using processing.js.

I'd expect the following to print 2.44 but instead prints 02


void setup() {
  float chips = 2.4355677;
  String chipsy = nfc(chips,2);
  println(chipsy);
}

void draw() {
}

Any ideas?

Will

Answers

  • edited April 2014 Answer ✓

    There are 4 number formatting styles in Processing:

    1. nf() http://processing.org/reference/nf_.html
    2. nfc() http://processing.org/reference/nfc_.html
    3. nfp() http://processing.org/reference/nfp_.html
    4. nfs() http://processing.org/reference/nfs_.html

    They're overloaded to format integers w/ 2 parameters and fractions w/ 3 parameters.
    All but nfc(), which expects 1 parameter only for integers and 2 for fractions.

    Apparently, the PJS folks got that confused in v1.4.1! Converter nfc() still uses the general # of parameters like the other 3! @-)

    p.nfc = function(value, leftDigits, rightDigits) {
      return nfCore(value, "", "-", leftDigits, rightDigits, ",")
    };
    

    Even though it's fixed in latest v1.4.7, that 1 isn't available in JS Mode yet! Much less on PJS code hosting sites! :o3

    nfc: function(value, rightDigits) {
      return CommonFunctions.nfCore(value, "", "-", 0, rightDigits, ",");
    },
    

    Therefore, when 1 or 2 parameters are passed in JS Mode, it guesses it's supposed to be integer rather than fraction formatting!
    Passing 3 parameters would fix the PJS side, but wouldn't compile in Java mode! :-<

  • Thanks so much, this is fantastically useful

  • I only need it to work in PJS so BONUS!

Sign In or Register to comment.