gpt4 book ai didi

VBScript Division - 自动将结果四舍五入到小数点后两位

转载 作者:行者123 更新时间:2023-12-05 08:02:39 26 4
gpt4 key购买 nike

我正在尝试将十进制数转换为十六进制,内置的十六进制函数限制为 8 个字符,因此我不得不编写自己的函数,但是 VBScript 似乎将数字四舍五入到小数点后两位。

例如。

106681252129194/16 = 6667578258074.625

问题是当我在 VBScript 中执行此代码时

strResult = 106681252129194 / 16

结果是 6667578258074.63,这使得计算十六进制数字成为一个小问题。

最佳答案

您必须进行整数除法和余数运算。受邀查看我对数字 (double < 2^50) to ascii 函数的尝试:

Function ntoa( nNum, iBase )
ntoa = "0"
If nNum Then
ntoa = Mid( "-", Sgn( nNum ) + 2 ) + ntoaPos( Abs( nNum ), iBase )
End If
End Function
Function ntoaPos( nNum, iBase )
If nNum >= 1 Then
Dim nD : nD = Fix( nNum / iBase )
Dim nM : nM = nNum - nD * iBase
ntoaPos = ntoaPos( nD, iBase ) _
& Mid( "0123456789ABCDEFGHIJKLMNOPQRSTUV", 1 + nM, 1 )
End If
End Function

基本测试代码:

  Dim aTests : aTests = Array( _
"zero (default: long)", 0 _
, "zero (Byte)" , CByte( 0 ) _
, "min_long" , CLng( -2^31 ) _
, "max_long" , CLng( 2^31-1 ) _
, "min_long - 1" , -2^31-1 _
, "max_long + 1" , 2^31 _
, "max_ulong" , 4294967295 _
, "big" , 2^40 _
, "bigger" , 2^45 _
, "bigger-1" , 2^45-1 _
, "bigger-11" , 2^45-11 _
, "very big" , 2^50 _
)

Dim nTest
For nTest = 0 To UBound( aTests ) Step 2
WScript.Echo "-----", nTest \ 2, aTests( nTest )
Dim nVal : nVal = aTests( nTest + 1 )
WScript.Echo " Type:", TypeName( nVal ), VarType( nVal )
WScript.Echo " Value:", nVal
WScript.Echo " hexxed:", ntoa( nVal, 16 )
WScript.Echo " decced:", ntoa( nVal, 10 )
WScript.Echo " abced:", ntoa( nVal, 32 )
Next

输出:

----- 0 zero (default: long)
Type: Integer 2
Value: 0
hexxed: 0
decced: 0
abced: 0
----- 1 zero (Byte)
Type: Byte 17
Value: 0
hexxed: 0
decced: 0
abced: 0
----- 2 min_long
Type: Long 3
Value: -2147483648
hexxed: -80000000
decced: -2147483648
abced: -2000000
----- 3 max_long
Type: Long 3
Value: 2147483647
hexxed: 7FFFFFFF
decced: 2147483647
abced: 1VVVVVV
----- 4 min_long - 1
Type: Double 5
Value: -2147483649
hexxed: -80000001
decced: -2147483649
abced: -2000001
----- 5 max_long + 1
Type: Double 5
Value: 2147483648
hexxed: 80000000
decced: 2147483648
abced: 2000000
----- 6 max_ulong
Type: Double 5
Value: 4294967295
hexxed: FFFFFFFF
decced: 4294967295
abced: 3VVVVVV
----- 7 big
Type: Double 5
Value: 1099511627776
hexxed: 10000000000
decced: 1099511627776
abced: 100000000
----- 8 bigger
Type: Double 5
Value: 35184372088832
hexxed: 200000000000
decced: 35184372088832
abced: 1000000000
----- 9 bigger-1
Type: Double 5
Value: 35184372088831
hexxed: 1FFFFFFFFFFF
decced: 35184372088831
abced: VVVVVVVVV
----- 10 bigger-11
Type: Double 5
Value: 35184372088821
hexxed: 1FFFFFFFFFF5
decced: 35184372088821
abced: VVVVVVVVL
----- 11 very big
Type: Double 5
Value: 1,12589990684262E+15
hexxed: 4000000000000
decced: 1125899906842624
abced: 10000000000

顺便说一句 - 你不能使用\:

>> a = 2 ^ 32
>> WScript.Echo a
>> b = a / 2
>> WScript.Echo b
>> c = a \ 2
>> WScript.Echo c
>>
4294967296
2147483648
Error Number: 6
Error Description: Overflow

关于VBScript Division - 自动将结果四舍五入到小数点后两位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5595173/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com