2014-01-14 94 views
0

我試圖十進制轉換爲特定的科學記數法格式:十進制轉換到特定的科學記數法格式

-2.802479940 ==> -.2802479940E + 01 3.796137791 ==> 0.3796137791E + 01

等。基本上,在負數上沒有前導零,並且在正數上沒有前導零 。我已經能夠在E大學科學記數法 中打印出來,但不知道如何強制只有' - '或'0'的前導部分。

+1

不知道你會怎麼做COBOL。 (你正在COBOL寫作,對不對?) –

回答

0

這是一種醜陋的(沒有任何內置string-formatting options吧,據我所知),但它應該做的你希望它做的:

import re 

nums = [-2.802479940, 3.796137791, -0.012, 0.035, 0, 100, -200] 
decimal_digits = 10 
for n in nums: 
    num = ('{:.'+str(decimal_digits-1)+'E}').format(n) 

    # increment exponent 
    exp_search = re.search(r'E(\+|-)\d+', num).group() 
    sign = exp_search[1] 
    exp = int(exp_search[1:]) 
    new_exp = exp + 1 
    exponent = 'E'+sign+'{:02}'.format(abs(new_exp)) 

    # move decimal place over 
    sig_search = re.search(r'-?\d\.\d+', num).group() 
    stripped = sig_search.replace('.', '') 
    if stripped[0] == '-': 
     significand = stripped.replace('-', '-.') 
    else: 
     significand = '0.' + stripped 

    # glue significand and exponent back together 
    new_num = significand + exponent 
    print('{:>20}{:>20}'.format(num, new_num)) 
    assert float(num) == float(new_num) 

輸出:

-2.802479940E+00 -.2802479940E+01 
    3.796137791E+00 0.3796137791E+01 
    -1.200000000E-02 -.1200000000E-01 
    3.500000000E-02 0.3500000000E-01 
    0.000000000E+00 0.0000000000E+01 
    1.000000000E+02 0.1000000000E+03 
    -2.000000000E+02 -.2000000000E+03 

這並不執行任何算術,只是字符串操作s,所以它不應該引入浮點表示或任何其他問題。

0

這裏的C代碼,做你想做什麼:

void numfmt(double d, char* buf) { 
    int exp=0; 
    int cont=1; 
    char sign = '0'; 
    if (d < 0) { 
     sign = '-'; 
     d = -d; 
    } 
    while(cont) { 
     if (d >= 1.0) { 
      exp ++; 
      d /= 10.0; 
     } else if (d < 0.1) { 
      exp --; 
      d *= 10.0; 
     } else 
      cont = 0; 
    } 
    sprintf(buf,"%12.10fE%+03d",d,exp); 
    *buf = sign; 
} 
+0

我很笨。這是我可以使用的C中的一個很好的答案,但我的意思是python3。我認爲,有一些簡單的方法可以在python3中執行此操作。我已經使用'%.10E'來將小數轉換爲科學記數法,但需要(如前所述)在小數點左側沒有數字,負數只有-.231etc,正數只有0.231etc。事實上,我已經以一種醜陋的方式做到了這一點,而這可能需要很長時間才能發佈。 – user3195765

+0

我爲你的問題添加了一個python標籤。 – stark

0

要將任何數字轉換爲科學記數法,您需要知道兩件事情,即指數和小數部分。像這樣:Number = Dec E指數(或者Number = Dec * 10^exponent)

Log10()函數將會非常重要,因爲它會給出Number的精確指數!然後得到的指數,你有這樣的關係:

exponent = log10(Number/Dec) 

,但不知道該月的一部分,我們將簡化成這種關係:

exponent = log10(Number) 

這不會給整數指數,只有指數爲其中1/10^exponent = Number。要獲得正確的整數指數,您需要使用floor()函數獲取第一個最低的整數。
這就是爲什麼你需要改變的關係:然後使用這個指數

exponent = int(floor(log10(Number))) # using int will convert any floating value into an integer exponent 

你可以很容易找到的關係小數部分:

Dec = Number/10^exponent 

現在你有辦法改變任何數字*變成科學記數法,如果你需要有一個前導零,那麼你只需要減少指數^^ * Log(x)定義在R * +上,所以如果你有一個負數,你必須將它轉換成在將它作爲參數傳遞給log10()之前是一個正數,如果數字是0,那麼您應該創建一個例外(返程DEC = 0和指數= 0)

這是代碼在python一個例子:

Decompose(originalvalue): 

    calcul = originalvalue; 
    if (originalvalue != 0.0): 
     if (originalvalue < 0): 
      calcul = -calcul; 
     exponent = int(floor(log10(calcul))); # get the integer exponent 
     value = originalvalue/10.0**exponent; # get scientific decimal part 
     # then to have a leading zero, you just have to verifiy if dec >= 1 or not, and in this case, modify the value and exponent to eliminate it. 
     if (value >= 1): 
      value *= 10**-1 
      exponent += 1 
    else: 
     exponent = 0; 
     value = 0.0; 
    return [value, exponent]; 
0

這裏是我想出了,有同樣的問題的解決方案。

def converter(number): 

    number_str = '{:.14e}'.format(number) 
    number_split = number_str.split('.') 
    int_part = number_split[0] 
    exp_part = number_split[1][-3:] 
    dec_part = number_split[1][:-6] 
    int_part_numeric = int(int_part) 
    if number == 0: 
     return '0.000000000000E+00' 
    if int_part_numeric == 0: 
     sign = '0' 
     new_exp_part = exp_part 
     new_dec_part = dec_part 
    elif int_part_numeric < 0: 
     sign = '-' 
     new_exp_part = '{:+03d}'.format(int(exp_part) + 1) 
     new_dec_part = int_part.strip('-') + dec_part[:-1] 
    elif int_part_numeric > 0: 
     sign = '0' 
     new_exp_part = '{:+03d}'.format(int(exp_part) + 1) 
     new_dec_part = int_part + dec_part[:-1] 

    return sign + '.' + new_dec_part + 'E' + new_exp_part 

它可能可能會簡化一點,雖然...

相關問題