0

how can I correct my method which is currently converting Bytes to Megabite into this form double 90979.32

this is my code:

 Public Function BytesToMegabytes(Bytes As Double) As Double
        '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
        dblAns = (Bytes / 1024) / 1024
        BytesToMegabytes = Format(dblAns, "###,###,##0.00")

    End Function

I finally need a string in the format: 90979'32. So I need it to be a single qoute on the format instead of the decimal point. How can I do this? Thank you all for your help!

Mara
  • 365
  • 1
  • 10
  • You could do a simple ````Replace()```` and just replace the period with a single quote. – JohnPete22 Jan 16 '20 at 15:26
  • Well your function returns a Double, not a String. A Double has no inherent format, so your question doesn't make sense. Put `Option Strict On` at the top of your code file. – djv Jan 16 '20 at 15:26

1 Answers1

3

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

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
  • This works, but I would be against putting everything inside the function called `BytesToMegabytes`. I wouldn't expect that to produce a string. – djv Jan 16 '20 at 15:30
  • 2
    @djv I agree. The OP may decide on a more appropriate function name after reading "A number only has a format like then it when it presented to the user as a string," or change the function to just divide by 2^20 and round to 2 d.p. – Andrew Morton Jan 16 '20 at 15:33
  • @AndrewMorton Thanks for your help, how can i just adjust the string format e.g. Show only three significant digits for the data volume, i.e. 0,xxx MB; x,xx MB; xx, x MB; xxx MB; x’xxx MB etc. – Mara Jan 22 '20 at 07:22
  • @Mara I added two methods to the answer. You might find FriendlyFileSizeFormat useful. – Andrew Morton Jan 22 '20 at 09:58
  • @Mara Scratch that - I answered your [new question](https://stackoverflow.com/q/59854707/1115360). – Andrew Morton Jan 22 '20 at 11:16