matlab裡面好像只能對總的有效位數進行控制,只對小數的話目前沒有太好的辦法。有些方法是可以用的,就是太複雜了,比如說方法1:digits+vpa 方法該方法是用digits()設定運算精度,然後用vpa獲得所要的小數位數。該方法可以達到獲取需要的任意位小數,但弊端很多,其1,運算精度裡包含了整數部分,所以需要提取整數部分和分數部分,並計算整數部分的位數,以下程式中求len就是整數位數。其2,獲得的結果是符號型,符號型的運算速度要比數值慢上很多。format longa = input("please give a number which will be changed£º");n = input("how many decimal digits will you keep: ");num = floor(a);str = num2str(num);len = length(str);err = a-num; % 獲取小數部分digits(n+len); % 此處為需要的小數位 + 整數位數need_num = num+vpa(err,n+len); %%%% 獲得按要求保留的小數,但獲得的是符號性need_str = num2str(double(need_num)) %%%%% 符號型轉浮點型,再轉字元型執行結果:please give a number which will be changed:3.1415926how many decimal digits will you keep: 2need_str =3.14方法2:round 函式法 該方法為先將要保留的部分轉成整數,即切除不要的尾部,然後再恢復成所要的相應小數。該方法最為簡潔,但不可恢復。format longa = input("please give a number which will be changed:");n = input("how many decimal digits will you keep: ");b = a*10^n;aa = round(b);need_num = aa/10^n; %%%%% 先化成整數,再轉成小數need_str = num2str(need_num)執行結果:please give a number which will be changed:pihow many decimal digits will you keep: 2need_str =3.14方法3:直接轉成字串,再取需要位數 該方法是直接轉成需要的字串,再擷取所要的小數位。缺點是連四捨五入都沒有,直接截斷尾部。format longa = input("please give a number which will be changed:");n = input("how many decimal digits will you keep: ");num = floor(a);str = num2str(num);len = length(str);str1 = num2str(a);need_str = str1(1:(len+n+1)) %%%取需要的長度,這裡需要加上整數長和小數點1位執行結果:please give a number which will be changed:pihow many decimal digits will you keep: 2need_str =3.14方法4:fprintf方法 該方法僅僅只是輸出顯示,並無法達到轉成字元的效果。而且無法動態控制。a = input("please give a number which will be changed:");n = input("how many decimal digits will you keep: ");fprintf("a = %3.2f\n",a) %%%%3.2f中.2就是小數位數執行結果:please give a number which will be changed:pihow many decimal digits will you keep: 2a = 3.14
matlab裡面好像只能對總的有效位數進行控制,只對小數的話目前沒有太好的辦法。有些方法是可以用的,就是太複雜了,比如說方法1:digits+vpa 方法該方法是用digits()設定運算精度,然後用vpa獲得所要的小數位數。該方法可以達到獲取需要的任意位小數,但弊端很多,其1,運算精度裡包含了整數部分,所以需要提取整數部分和分數部分,並計算整數部分的位數,以下程式中求len就是整數位數。其2,獲得的結果是符號型,符號型的運算速度要比數值慢上很多。format longa = input("please give a number which will be changed£º");n = input("how many decimal digits will you keep: ");num = floor(a);str = num2str(num);len = length(str);err = a-num; % 獲取小數部分digits(n+len); % 此處為需要的小數位 + 整數位數need_num = num+vpa(err,n+len); %%%% 獲得按要求保留的小數,但獲得的是符號性need_str = num2str(double(need_num)) %%%%% 符號型轉浮點型,再轉字元型執行結果:please give a number which will be changed:3.1415926how many decimal digits will you keep: 2need_str =3.14方法2:round 函式法 該方法為先將要保留的部分轉成整數,即切除不要的尾部,然後再恢復成所要的相應小數。該方法最為簡潔,但不可恢復。format longa = input("please give a number which will be changed:");n = input("how many decimal digits will you keep: ");b = a*10^n;aa = round(b);need_num = aa/10^n; %%%%% 先化成整數,再轉成小數need_str = num2str(need_num)執行結果:please give a number which will be changed:pihow many decimal digits will you keep: 2need_str =3.14方法3:直接轉成字串,再取需要位數 該方法是直接轉成需要的字串,再擷取所要的小數位。缺點是連四捨五入都沒有,直接截斷尾部。format longa = input("please give a number which will be changed:");n = input("how many decimal digits will you keep: ");num = floor(a);str = num2str(num);len = length(str);str1 = num2str(a);need_str = str1(1:(len+n+1)) %%%取需要的長度,這裡需要加上整數長和小數點1位執行結果:please give a number which will be changed:pihow many decimal digits will you keep: 2need_str =3.14方法4:fprintf方法 該方法僅僅只是輸出顯示,並無法達到轉成字元的效果。而且無法動態控制。a = input("please give a number which will be changed:");n = input("how many decimal digits will you keep: ");fprintf("a = %3.2f\n",a) %%%%3.2f中.2就是小數位數執行結果:please give a number which will be changed:pihow many decimal digits will you keep: 2a = 3.14