십진수를 기하 급수적으로 변환 할 수 있습니까?리눅스 bash에서 십진수를 지수로 변환하는 방법
11.2345 : 0.112345E + 2?
는
는 당신이 Cprintf
의 경우 같은 서식을 제공 할 수 있습니다
십진수를 기하 급수적으로 변환 할 수 있습니까?리눅스 bash에서 십진수를 지수로 변환하는 방법
11.2345 : 0.112345E + 2?
는
는 당신이 Cprintf
의 경우 같은 서식을 제공 할 수 있습니다
사용 printf
명령, 거기에 당신이하는 %e
옵션을 주셔서 감사합니다 그 지수 표기법으로 인쇄 :
printf %e 11.2345
이를 결과는 다음과 같습니다.
1.123450e+01
(210)는 유용하게 printf '%e'
의 사용을 제안, 그러나 이것은 다음 의미를 있습니다
참고 :이 출력에 대문자E
를 사용하는 것을 제외하고 printf '%E'
이 본질적으로 동일하게 작동합니다.
는 관계없이 입력 번호를 가지고 얼마나 많은 유효 숫자의 기본적으로 출력에서 소수점을 사용합니다.
printf %e 1
이 1.000000e+00
printf %.1e 129
생산량 1.3e+02
그러나출력 수가 변함 유효수합니다 (e
/E
앞의 숫자)의 정수부 >= 1
및 < 9
및 지수를 갖는다 정규화 표기법 사용 항상 2 차원 인 명시적인 부호가있는 10 진수 (정수) :
printf %.1e 66
는0.
로를 시작으로, 유효 숫자에 다른 정상화을 적용 할 수없는 방법이 있습니다 (영어 로케일) 6.6e+01
를 얻을 수 있습니다.printf %e
면 입력 번호가 모두되어야 함을 의미 현재 지역 - 출력 될 것이다 - 특히 진수 표시에 관하여, 현재 지역의 규칙에 따라 형식 (.
대 ,
) 및 천 단위 구분 기호 (,
대 .
대 <space>
).
(export LC_ALL=de_DE.UTF-8; printf %.2e 66,1)
독일 로케일을 사용 6,61e+01
을 수득 - IN- 출력 모두에 소수 마크로 ,
의 사용을주의.찾기 로케일 인식 배쉬 기능이 제한 일부를 해결 바닥에 toNumSci()
; 구체적으로 말하자면, 중요한 숫자의 입력 숫자를 보존하고 유효 숫자 정규화를 0.d...
으로 선택할 수 있습니다.
$ toSciNum 11.2345 1 # 1 opts into `0.d...` significand normalization
0.112345E+02 # same number of significant digits, 0.d...-normalized
참고 : 질문의 출력 형식의 유일한 차이는 두 자리를 사용하는 것입니다 왼쪽 패딩 - - 제로와 멱지수.
소스 코드 toNumSci()
의 :
#!/usr/bin/env bash
# Usage:
# toSciNum number [zeroDotFormat [numSigDigits]]
# Converts the specified number (float or integer) to normalized scientific
# notation ([+-]d.d...E+-dd), preserving the number of significant digits in
# the input.
# Optionally you can control the number of significant digits explicitly
# and choose alternative output format [+-]0.d...E+-dd
# Note: The input number must use locale-appropriate formatting with
# respect to decimal mark and thousands grouping, if applicable.
# Similarly, the output number is formatted locale-appropriately.
# Examples:
# toSciNum 123 # -> 1.23E+02
# toSciNum 123 1 # -> 0.123E+03
# toSciNum -66.7 0 4 # -> -6.670+01
toSciNum() {
local num=$1 zeroDotFormat=${2:-0} numSigDigits=$3
local digitsOnly fmtStr numSci decMark significand exponent sign=
# Determine the number of significant digits.
# If not specified, use the same number as in the input number.
if [[ -z $numSigDigits ]]; then
digitsOnly=${num//[!0-9]} # Remove all non-digit characters...
numSigDigits=${#digitsOnly} # ... and count them.
fi
# Construct the printf format string.
# Note that the number of decimal places is the number of
# significant digits minus 1, because in the normalized scientific notation
# that %e/%E produce, there by definition always 1 integer digit.
fmtStr="%.$((numSigDigits - 1))E"
# Create the normalized scientific notation representation (d.dddd ...)
# and store it in var. $numSci.
printf -v numSci "$fmtStr" "$num"
# If the 0.ddd format is requested instead, transform the result.
if ((zeroDotFormat)); then
[[ $numSci == -* ]] && sign='-'
# Extract the decimal mark from the result.
decMark=${numSci:1:1}
# Extract the exponent from the result
significand=${numSci%E*}
exponent=${numSci##*E}
# Construct the new significand as [-]0.ddd
significand="${sign}0${decMark}${significand//[!0-9]}"
# Construct the new exponent (to which +1 must be added to compensate, now
# that the significand is effectively being divided by 10.
printf -v exponent '%+03d' $((1 + 10#${exponent}))
# Assemble the pieces to form the new number.
numSci="${significand}E${exponent}"
fi
printf '%s\n' "$numSci"
}
모자입니다. –
보십시오 : 매끄러운 구현 오프'의 printf "% 전자"11.2345' – sat