仅显示数据量的三位有效数字 [英] Show only three significant digits for the data volume
问题描述
如何只显示数据量的三位有效数字,即0,xxx;x,xx;xx,x;xxx;x’xxx 等
how can I Show only three significant digits for the data volume, i.e. 0,xxx; x,xx; xx,x; xxx; x’xxx etc.
这是我的代码
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
输出:目前我的得分为 8,798'54 MB.
要达到 8'798 MB,我怎样才能得到它?
to be 8'798 MB, how can I get it?
谢谢大家的帮助
推荐答案
从您的上一个问题...
使用有效数字代替小数位:
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
例如,Console.WriteLine(BytesToMegabytes(9225936896))
输出
8'790
如果将行 Dim SignificantFigures = 3
更改为 Dim SignificantFigures = 4
,它会输出
If you change the line Dim significantFigures = 3
to Dim significantFigures = 4
, it outputs
8'798
根据您想要的舍入,您可能需要使用 dblAns = Math.Round(dblAns/v, MidpointRounding.AwayFromZero) * v
代替,或者 Math.Ceiling
而不是 Math.Floor
.
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
.
这篇关于仅显示数据量的三位有效数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!