A number only has a format like that when it is presented to the user as a string. Until then it is just a number.
The .ToString
method can take a parameter which tells it which culture to use for the formatting.
We can take advantage of that by using an existing culture and modifying it to use an apostrophe as the decimal separator:
Option Strict On
Imports System.Globalization
Module Module1
Public Function BytesToMegabytes(Bytes As Long) As String
'This function gives an estimate to two decimal
'places. For a more precise answer, format to
'more decimal places or just return dblAns
Dim dblAns As Double = (Bytes / 1024) / 1024
Dim ci = New CultureInfo("en-GB")
ci.NumberFormat.NumberDecimalSeparator = "'"
Return dblAns.ToString("###,###,##0.00", ci)
End Function
Sub Main()
Console.WriteLine(BytesToMegabytes(123445568999))
Console.ReadLine()
End Sub
End Module
Outputs:
117,726'87
You probably know the code for the culture that you want to use, so if you used that instead of "en-GB" then there would be no need to set the NumberDecimalSeparator.
If you don't want the commas for number grouping, add in
ci.NumberFormat.NumberGroupSeparator = ""
Note that the Long
type is probably better if you're dealing with bytes. The type of the returned value from a function must be the same as in the declaration: using Option Strict On
will help you with things like that, and you should set it as the default for new VB.NET projects.
Ref: NumberFormatInfo Class