Decimal formatting issue

dim a as double = 100
system.DebugLog format(a, “#.##”)
'output is 100 would expect 100

but in csharp (and all languages I’ve ever used that have formatting)

decimal a = 100;
string s = string.Format("{0:#.##}", a);
//output is 100 which is what I would expect

Any idea what I’m doing wrong, or is this a bug?

Since you explicitly require the dot in your format “#.##” Xojo faithfully places it. I do not think it is a bug, more maybe a peculiarity of its particular treatment of Format as opposed to the other languages you are accustomed to.

It is easy, though, to create a method that will react as you expect :

[code]Function Forma(Value as double, formatstring as string) As String
if floor(value) = value and left(right(formatstring, 3),1) = “.” then
formatstring = left(formatstring, len(formatstring)-3)
end if

return format(value, formatstring)
End Function
[/code]

Thanks Michael.

I’ve done this as a temporary fix:

[code]Function FormatFixed(Value As Double, Format As String) As String
Dim tmp as String

tmp = format(Value, Format)

if right(tmp,1) = “.” then
return left(tmp,len(tmp)-1)
end if

return tmp
End Function[/code]

I don’t explicitly require a dot, the manual says “.” is a “Placeholder for the position of the decimal point.”

There is no decimal point, because its not a decimal value.

The decimal should only be shown if there is a number after the decimal, i.e. when using #.00 - 100.00

As long as its not a glaring error on my part, I’ll see if someone from Xojo comments on this later, if they don’t I’ll pop it into feedback and see what they say about it.

Thanks again.

Indeed you should file a feature request.

About the explicit notion. If you read carefully the LR, you will see that pretty much only # and , are not explicit (will not appear if the number is shorter). All others like 0, dot, etc. Never mention something like “displays the digit from the value if it is present.”. So I believe Xojo did not envision the possibility of implicit dot. The notion of placeholder being here that the character WILL appear where its place is marked.

FTIW I use these similar routines to display Integers and decimals:

[code]Function getLargeIntegerWAD(largeNumber As Int64, decimalPoints As Integer = 0, formatString As String = “####################0”) As String
Dim returnResult As String

if decimalPoints > 0 and instr(formatString, “.”) = 0 then 'don’t add two periods!
formatString = formatString + “.”
for tempInt As Integer = 0 to decimalPoints
formatString = formatString + “0”
next
end if

returnResult = Format(largeNumber, formatString)
if instr(formatString, “.”) > 0 then 'don’t remove the final zero if it’s not a decimal!
while right(returnResult, 1) = “0”
returnResult = left(returnResult, len(returnResult) - 1) 'remove trailing zeros
wend
end if
if right(returnResult, 1) = “.” then returnResult = left(returnResult, len(returnResult) - 1) 'remove trailing period

Return returnResult

End Function
[/code]

[code]Function getLargeDecimalWAD(largeNumber As Double, decimalPoints As Integer = 0, formatString As String = “###,###,###,###,###,###,##0”) As String
Dim returnResult As String

if decimalPoints > 0 and instr(formatString, “.”) = 0 then 'don’t add two periods!
formatString = formatString + “.”
for tempInt As Integer = 0 to decimalPoints
formatString = formatString + “0”
next
end if

returnResult = Format(largeNumber, formatString)
if instr(formatString, “.”) > 0 then 'don’t remove the final zero if it’s not a decimal!
while right(returnResult, 1) = “0”
returnResult = left(returnResult, len(returnResult) - 1) 'remove trailing zeros
wend
end if
if right(returnResult, 1) = “.” then returnResult = left(returnResult, len(returnResult) - 1) 'remove trailing period

Return returnResult

End Function
[/code]

Here’s a link to the bug report if someone stumbles across this post <https://xojo.com/issue/44624>

While the format() function is certainly very limited, I wouldn’t necessarily call this a bug. In any kind of scientific writing a floating point number is practically always formatted with a decimal point, while integers are not. (I generally use the Str() function to format integers). As many others have done, I’ve also created my own customized versions of the format function to handle the cases where the original is inadequate. In my opinion, allowing the format function to decide when and when not to include a decimal point would simply make it too unpredictable to be useful.

Verified on 2016 and still noot fixed :frowning: