ToString function bug?

Support forum for MegunoLink
Post Reply
bittware
Posts: 15
Joined: Tue Jan 17, 2017 8:40 am

Tue Feb 21, 2017 8:24 am

Hi,
When I use the expressioin of [Tools.ToInt(IPNumericUpDown1.Value*10).ToString("X4")] within control panel. I expected it works for both positive and negative numbers.
E.g. +1.5, it gave 000F, very good, no problem.
But when it came to negative numer, namely -1.5, it gave FFFFFFF1, eight characters instead of 4 that was explicitly required in the expression.
Could you help to confirm on this?
If this is really a bug, how can I work around it?
Thanks.
Paul
Posts: 33
Joined: Wed Jun 10, 2015 10:35 pm

Tue Feb 21, 2017 9:39 pm

Howdy,

MegunoLink is built on C# and we are using that for formatting. In C# the width specifier sets the minimum width of the output. The output will expand to whatever is necessary to correctly represent the value. And an int is a 32 bit number so negative values will take 8 characters.

To solve this you can cast the integer to a 16 bit integer:
[((Int16)Tools.ToInt(n.Value*10)).ToString("X4")]

Hope this helps,
Paul.
Post Reply