str single with format (bug?)

Is this a bug or am I missing something?

[code] Dim s as single = 3/7
Dim d as double = 3/7

Dim t1 as string = str(s, “-0.###”)
Dim t2 as string = str(d, “-0.###”)

break[/code]

After the break:

t1 = 0.4285714 (with 7 decimals!!!)
t2 = 0.429 (correctly 3 decimals)

If a single doesn’t work, a double no problem.

I’m not sure that # limits decimal places on the right side of the decimal point. Have you tried “-0.000” ?

0 instead of # does indeed work for this, I’ve used it many times now.

No. This is not the solution.

[code] Dim s as single = 3/7
Dim d as double = 3/7

Dim t1 as string = str(s, “-0.###”)
Dim t2 as string = str(d, “-0.###”)
Dim t3 as string = str(s, “-0.000”)
Dim t4 as string = str(d, “-0.000”)

break[/code]
gives:
t1 = 0.4285714 (with 7 decimals!!!)
t2 = 0.429 (correctly 3 decimals)
t3 = 0.4285714 (with 7 decimals!!!)
t4 = 0.429 (correctly 3 decimals)

Besides, # and 0 are not the same. I want to use # because it writes nothing when you have zeros at the right end.

[quote=157994:@Greg O’Lone]I’m not sure that # limits decimal places on the right side of the decimal point. Have you tried “-0.000” ?[/quote][quote]LR:
Str( 95.65, “##.##” ) would use the “.” as the decimal separator no matter what the user’s locale setting is.[/quote]

There is already a feedback://showreport?report_id=20342 from 2012.

[quote=157992:@Ramon SASTRE]Is this a bug or am I missing something?

[code] Dim s as single = 3/7
Dim d as double = 3/7

Dim t1 as string = str(s, “-0.###”)
Dim t2 as string = str(d, “-0.###”)

break[/code]

After the break:

t1 = 0.4285714 (with 7 decimals!!!)
t2 = 0.429 (correctly 3 decimals)

If a single doesn’t work, a double no problem.[/quote]

Use the new framework :

[code] Dim s as single = 3/7
Dim d as double = 3/7

Dim t1 as string = s.ToText(Xojo.Core.Locale.Current, “#,###.###”)
Dim t2 as string = d.ToText(Xojo.Core.Locale.Current, “#,###.###”)

break[/code]

Thank you Michel for your workaround.
But this is not the solution, as I will explain later.
In fact it’s much easier to declare the variable as double and everything is solved. That is what I have done and now everything works, however there is a bug (that is obvious) that must be solved or at least it should be warned at the LR. I lost some hours finding the problem, because I never thought that something so simple could give any problem.
About your proposal:

  1. At the help we can read

[quote]Methods
ToText(Extends value As Single, Optional locale As Locale) As Text
Converts a Double value to a Text value using the specified locale.

Examples
Dim s As Single = 123.45

Dim t As Text
t = d.ToText // t = “123.45”[/quote]
incongruently uses Single but explains: Converts a Double…

  1. There is no reference to the format string, but after using

Dim t0 as String = s.ToText(Xojo.Core.Locale.Current, "-0.###") Dim t1 as string = str(s, "-0.###") Dim t2 as string = str(d, "-0.###")
I get:

t0 = -0,429 (negative and comma as decimal separator)
t1 = 0.4285714 (with 7 decimals!!!)
t2 = 0.429 (correctly 3 decimals)

It is really a chaos!!!

[quote=158072:@Ramon SASTRE]Thank you Michel for your workaround.
But this is not the solution, as I will explain later.
In fact it’s much easier to declare the variable as double and everything is solved. That is what I have done and now everything works, however there is a bug (that is obvious) that must be solved or at least it should be warned at the LR. I lost some hours finding the problem, because I never thought that something so simple could give any problem.
About your proposal:

  1. At the help we can read

incongruently uses Single but explains: Converts a Double…

  1. There is no reference to the format string, but after using

Dim t0 as String = s.ToText(Xojo.Core.Locale.Current, "-0.###") Dim t1 as string = str(s, "-0.###") Dim t2 as string = str(d, "-0.###")
I get:

t0 = -0,429 (negative and comma as decimal separator)
t1 = 0.4285714 (with 7 decimals!!!)
t2 = 0.429 (correctly 3 decimals)

It is really a chaos!!![/quote]

Take that post and file a specific Documentation bug report with a copy/paste. I am sure Paul lefebvre will be glad to sort this out.