The Format Number Template

john's Avatar

john

25 Feb, 2015 07:56 AM

One of the biggest hurdles I've encountered in learning Nodebox is the paucity of documentation. The node reference offers little more than a bare list of ports. Thank goodness for the tutorials and the generous people on the forum!

The most frustrating node for me was Format Number. All it says about how to format numbers is "Format: The format template". That's it. Not even a clue as to which of the many different format coding systems NodeBox uses. When I needed to format some numbers to always show a + sign, add commas, and put a percentage sign at the end I tried many guesses at different incantations without ever hitting on the magic formula.

The only clue was the percentage sign at the front of the hint text of the template field. This rang a distant bell. A long web search eventually turned up the old string format operator used in early versions of Python (pre-2.6). I now *think* this is what Nodebox still uses.

Here, for fellow newbies, is an old web page which documents this system:

http://infohost.nmt.edu/tcc/help/pubs/python27/web/old-str-format.html

Could you confirm that this is correct? If it is, perhaps you could add this (or some better) link to the documentation for the Format Number node.

Thanks!

John

  1. Support Staff 1 Posted by Frederik De Ble... on 25 Feb, 2015 08:35 PM

    Frederik De Bleser's Avatar

    You're right, we could do a lot better in that regard.

    The format number node uses Java's String.format function. Here is the documentation. I've added the link on the reference as well.

    However, we can still do better, showing some examples of where and how you might use it. An example of where the documentation is a bit longer is the keys node.

  2. Support Staff 2 Posted by john on 26 Feb, 2015 02:16 AM

    john's Avatar

    Thanks Frederik!

    One thing. Since the Format Number node insists on a floating point input, it doesn't seem to allow any non-floating conversions. So even if I feed it an integer, it won't allow '%x' to convert it to hex.

    Is this correct? If so, is there some other (easy) way to convert an integer to a hex value? I could do this by stringing together mods, substrings, and a concatenate or by making a custom node but I'd hoped for something less messy.

    Thanks,

    John

  3. Support Staff 3 Posted by Frederik De Ble... on 28 Feb, 2015 10:13 AM

    Frederik De Bleser's Avatar

    That’s tricky. The format number always converts to floating-point first, internally, so there’s no good way to use that.

    I think your best bet is to write a custom node. The code for this (in Python) is rather simple

    def to_hex(v):
        return hex(v)

    This will return something like “0xab456”. If you don’t want the “0x” at the front, use:

    def to_hex(v):
        return hex(v)[2:]

    Grtz,

    F

  4. john closed this discussion on 02 Mar, 2015 03:40 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac

Recent Discussions

22 Feb, 2024 05:51 AM
22 Feb, 2024 01:11 AM
21 Feb, 2024 05:27 PM
21 Feb, 2024 04:49 PM
16 Feb, 2024 05:24 PM

 

06 Feb, 2024 04:45 AM
19 Jan, 2024 09:50 AM
13 Jan, 2024 03:47 AM
05 Jan, 2024 05:10 PM
05 Jan, 2024 04:54 PM
04 Jan, 2024 11:49 PM