如何解决如何在excel中格式化复数?
我在 Excel 中创建复数,例如使用 my_funct
。但是,如果实部或虚部为 0,则它会被丢弃,例如 0.500 而不是 0.500 + 0.000i 或 0.800i 而不是 0.000 + 0.800i。在桌子上看起来很糟糕。使用 =COMPLEX(ROUND(A10;3);ROUND(B10;3))
而不是 FIXED
给出相同的结果。
我怎样才能正确格式化?
提前致谢 恩格尔伯特
解决方法
根据我在上面评论中链接的答案,这里是一个 UDF 示例,它将格式化复数并包含零值。此函数的好处是它可以处理 Range
输入(作为单个单元格)或数值。
Option Explicit
Public Function FormatComplex(r As Variant,_
Optional i As Variant,_
Optional fmt As String = "0.000") As String
'--- returns a formatting string depicting the complex number
' represented by r and i. these parameters may be given as
' a (single cell) range,or a value
' INPUTS: r can be a (single-cell) Range or a value represented
' as a string,double,integer,or complex value
' i can be a (single-cell) Range or a value represented
' as a string,or complex value
' (should be omitted if "r" is a Complex value)
' fmt is a VBA style format string (cannot be used if
' the "r" parameter is Complex)
Dim realPart As Double
Dim imgPart As Double
If TypeName(r) = "Range" Then
'--- must be a single cell
If r.Count > 1 Then
FormatComplex = CVErr(xlErrRef)
Exit Function
End If
If Right$(r,1) = "i" Then
'--- the value given is already assumed to be complex,so
' split up the parts here
realPart = Application.WorksheetFunction.ImReal(r)
imgPart = Application.WorksheetFunction.Imaginary(r)
Else
realPart = r.Value
End If
Else
realPart = r
End If
If Not IsMissing(i) Then
If TypeName(i) = "Range" Then
'--- must be a single cell
If i.Count > 1 Then
FormatComplex = CVErr(xlErrRef)
Exit Function
End If
imgPart = i.Value
Else
imgPart = i
End If
End If
Dim result As String
result = Format(realPart,fmt)
If Left$(fmt,1) <> "+" Then
'--- if the user-specified format string does not explicitly
' call out a sign,then we have to add it ourselves
' this might be desirable if the user does not want a
' leading "+" in front of the real part
If imgPart >= 0 Then
result = result & "+"
Else
'--- assume the "-" is included in the number formatting
' so don't add it here
End If
End If
result = result & Format(imgPart,fmt) & "i"
FormatComplex = result
End Function
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。