string formatting in zscript

Mon Sep 13, 2021 10:54 am

Hello all, I need a simple advice here.

How do I format a string, composed of a pre-defined string and a calculated integer? Let's say I need to display a value on statusbar, but I want the number to be preceeded by a number.
I have this code here used to display item weights in opened inventory bar:
if (item.Mass > 0) {
   DrawString(mYelFont, FormatNumber(item.Mass, 3, 5), (103 + 35*i, 38), DI_TEXT_ALIGN_RIGHT, Font.CR_GREEN);

With "FormatNumber(item.Mass, 3, 5)" resulting in simple number like '27'.
All I need to know is how do defined the string to display 'W: 27'?
I just don't know how to define the string with pre-defined "W:" and the FormatNumber(item.Mass, 3, 5) following.
I'm little slow in this part and zscript offers toomuch possibilities on how to achieve this, so I'm asking for an advice on how to make this.

Re: string formatting in zscript

Mon Sep 13, 2021 11:01 am

With ZScript you can combine strings by putting ".." between them.
For example you could try this:

Re: string formatting in zscript

Mon Sep 13, 2021 11:08 am

Many thanks, jarewill. Exactly what I was looking for.

But I need to note one pretty important thing. Took me a while to notice that it's not working as intended because the font I use for the small number lacks, if you use this, use a font containing both letters and numbers. Otherwise it will just not work.


Ummm....I really, really, really have to start reading documentation. Or even the hints displayed by slade... :D
The function FormatNumber() has the string prefix built in... :shock: :D

FormatNumber(item.Mass, 3, 5, 0, "W:")

Silly me! :D