0

how can I Show only three significant digits for the data volume, i.e. 0,xxx; x,xx; xx,x; xxx; x’xxx etc.

this is my code

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(9225936896))

        Console.ReadLine()

    End Sub

End Module

Outputs: currently I score 8,798'54 MB.

to be 8'798 MB, how can I get it?

thank you all for your help

Mara
  • 365
  • 1
  • 10
  • `Return dblAns.ToString("N3", ci)`. – Jimi Jan 22 '20 at 07:42
  • @Jimi to me with this comes the same format as with : Return dblAns.ToString("###,###,##0.00", ci) – Mara Jan 22 '20 at 07:56
  • What is your expected output? – jmcilhinney Jan 22 '20 at 08:24
  • `((123445568999 / 1024) / 1024).ToString("N3", ci)` prints `117,726'869` – Jimi Jan 22 '20 at 08:25
  • By the way, adding all those extra characters to the format specifier is pointless. "#,0.00" would have the same effect as what you have. You don't need to specify exactly where to put every group delimiter; just that group delimiters should be used. – jmcilhinney Jan 22 '20 at 08:26

1 Answers1

1

Following on from your previous question...

To use significant figures instead of decimal places:

Public Function BytesToMegabytes(bytes As Long) As String
    Dim dblAns As Double = (bytes / 1024) / 1024

    If dblAns = 0 Then
        Return "0"
    End If

    Dim significantFigures = 3
    Dim magnitude = Math.Floor(Math.Log10(dblAns))
    Dim v As Double = 10 ^ (magnitude - significantFigures + 1)
    dblAns = Math.Floor(dblAns / v) * v

    Dim ci = New CultureInfo("")
    ci.NumberFormat.NumberDecimalSeparator = ","
    ci.NumberFormat.NumberGroupSeparator = "'"

    Return dblAns.ToString("#,##0.###", ci)

End Function

For example, Console.WriteLine(BytesToMegabytes(9225936896)) outputs

8'790

If you change the line Dim significantFigures = 3 to Dim significantFigures = 4, it outputs

8'798

Depending on the rounding you want, you may want to use dblAns = Math.Round(dblAns / v, MidpointRounding.AwayFromZero) * v instead, or perhaps Math.Ceiling instead of Math.Floor.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
  • Thanks a lot for the answer, I only need the final output in MB, but from too many positions, for example for 9225936896 to be 8'798 MB, how can I get it? currently I score 8,798'54 MB. – Mara Jan 22 '20 at 12:19
  • @Mara There were conflicting numbers for the significant figures in the question, so I have added in how to change it. Do you also want the thousands separator to be `'`? – Andrew Morton Jan 22 '20 at 12:41
  • this already looks better to me, i wish it was a ' separator per thousand, as in the example: 8'798 MB – Mara Jan 22 '20 at 12:45
  • @Mara If you refer to my answer to your earlier question on the subject, you'll see I mentioned that you could set the value for `ci.NumberFormat.NumberGroupSeparator`. I've edited my answer accordingly, and I guess that the decimal separator is a comma `,`. – Andrew Morton Jan 22 '20 at 13:16