Some quirk when using Format vs .ToString

Look here (I am using Polish locale):

var d as double = -12666333
var s as String

s=Format(d,"-###,###,###,###.00")

MessageBox s

This will display “-12 666 333,00” which is correct since the value is below zero.

If you forget “-” in Format format string:

var d as double = -12666333
var s as String

s=Format(d,"###,###,###,###.00")

MessageBox s

…i will display “12 666 333,00” which is not correct since “-” character was omitted.

But if you use .ToString with format, the “-” is not needed to display negative numbers correctly:

var d as double = -12666333
var s as String

s=ToString(Locale.Current,"###,###,###,###.00")

MessageBox s

You will get correct “-12 666 333,00”.

The behaviour of using “-” in Format command was explained in Xojo documentation and it behaves exactly as described, i.e. you need to put that “-” in front of format string to get negative values displayed correctly.

In case of .ToString the documentation points to Unicode Number Format Patterns where it seems the need of placing “-” character stays the same:

"minusSign

  • Symbol used to denote negative value."

So, is Xojo correct or not?

I spotted this when my program starts to display i.e. “--12 666 333,00” instead of “-12 666 333,00” when using .ToString…

I’ve started a bug report a while ago. This is similar to my report:

Strings returned by Str( value, format ) and .ToString can return invalid values
https://tracker.xojo.com/xojoinc/xojo/-/issues/72757

The negative part of the mask is missing

Var mask as String = "###,###,###,###.00;-###,###,###,###.00"

Var d as double = -12666333.129

MessageBox "Format(val,""pos;neg""): "+Format(d, mask)+EndOfLine+_
"String.ToString(""pos;neg""): "+d.ToString(mask)+EndOfLine+_
"String.ToString(locale,""pos;neg""): "+d.ToString(Locale.Current, mask)

d = 12666333.129

MessageBox "Format(val,""pos;neg""): "+Format(d, mask)+EndOfLine+_
"String.ToString(""pos;neg""): "+d.ToString(mask)+EndOfLine+_
"String.ToString(locale,""pos;neg""): "+d.ToString(Locale.Current, mask)

Quit

image

image

https://unicode.org/reports/tr35/tr35-4.html#Number_Format_Patterns

1 Like

From: UTR #35: Locale Data Markup Language

A pattern contains a postive and negative subpattern, for example, “#,##0.00;(#,##0.00)”. Each subpattern has a prefix, a numeric part, and a suffix. If there is no explicit negative subpattern, the negative subpattern is the localized minus sign prefixed to the positive subpattern. That is, “0.00” alone is equivalent to “0.00;-0.00”.

You are only providing the positive pattern, and the Unicode format assumes that for negative patter you want the same but with the minus sign in front of it. So Xojo is correct. Format needs the - and ToString doesn’t.


Edit:

Also the Unicode page mentions:

Thus for consistency in the CLDR data, the following conventions should be observed:

  • All number patterns should be minimal: there should be no leading # marks except to specify the position of the grouping separators (e.g. avoid ##,##0.###).
  • All formats should have one 0 before the decimal point (e.g. avoid #,###.##)
  • Decimal formats should have three hash marks in the fractional position (e.g. #,##0.###).
  • Currency formats should have two zeros in the fractional position (e.g. ¤ #,##0.00).

They don’t recommend that format, you can use “#,##0.00” instead.

1 Like

While you’re knee deep in the Unicode doc, could you find out what it says about scientific notation? William recently changed ToString to make it so DateTime.Now.SecondsFrom1970.ToString produces an unusable scientific notation string.

Previously this method produced a string version of the number that was stored. Great, easy, fast, and simple for communicating with APIs. It now requires more boilerplate copypasta and the decision seems really short sighted. I’m still salty about it.

I’m now curious if this was designed correctly to begin with.

It seems due to some Xojo “Default” choice. If you override its default you can get it working as you may wish as

DateTime.Now.SecondsFrom1970.ToString("#0;-#0")
1 Like

so the conclusion is, quoting AlbertoD, the following:

Format needs the - and ToString doesn’t. Formatting string for .ToString should be “#,##0.00”

The real reason I posted this was me worrying “what if it was a mistake and they will repair it in the future without further notice?”

Did you read the rest of my post or did you just want to “um actually” me?

In my planet we don’t “um actually” people. We just expose useful information.

1 Like

Really? Because the way you target people doesn’t seem to agree.

Mind to explain?

My whole post, when not taken out of context, includes the information that it is indeed possible to get ToString to output the SecondsFrom1970 as a string useful for interacting with REST APIs.

Your post snips the one part of my post, ignoring the context I provided illustrating that it was still possible, in order to make it look like you were telling me something I didn’t already know.

I didn’t ask for any unsolicited code advice, I didn’t need it. I wanted to start a discussion about how ToString has become less useful in more recent releases. And you single-handedly stopped that (which is why I’m upset).

You are not the only reader of the forum. People having the same problem can make it work overriding whatever Xojo is using as default mask behind the scenes. You, or anyone, aren’t any kind of “target”.

1 Like

Your only goal was to make it look like you told me something. My post was relevant to ToString, not the actual format and you KNOW that. You missed the point, misquoted me, and made sure my discussion wouldn’t happen.

This looks like a conspiracy theorist view. I already said you are completely wrong about it.

My post was relevant to ToString, not the actual format and you KNOW that.

Well, at this point, I don’t care about your interpretations. Whatever makes you happy and calm.

You putting me on ignore would make me happy and calm. I’ve asked, but staff won’t do it and there’s no “block” function.

Gentlemen, I really wouldn’t start this thread knowing that it could end up like this… in my opinion, knowledge sharing, even redundant, is more important from our point of view than some awkwardness in speech, if it really happened.

1 Like

It’s not you, it’s Rick, and I get upset literally every time he does this. I’ve asked staff to put me on his ignore list since there’s no block function, but they refuse to help.

Knowledge, information, and sharing is important but Rick’s goals weren’t any of those. I would have been happy to help someone struggling learn how to use the format, but the specifics weren’t relevant to the point I was trying to make.