简体   繁体   English

为什么数值在MATLAB中以科学计数法存储?

[英]Why do the numerical values get stored in scientific notation in MATLAB?

I have tried to change the format to int but I will lose the accuracy. 我试图将格式更改为int但我将失去准确性。 I do not want to display the value in scientific notation. 我不想用科学记数法显示价值。

Any clue why does it save the values in scientific notation? 任何线索为什么它以科学记数法保存价值?

You can use the MATLAB function format to change how numbers are displayed by default format short , format long , etc. 您可以使用MATLAB函数format更改数字的显示方式,默认format shortformat long等。

If you want to display numbers with more control over the exact format, for example to control how many decimal points to display or to zero pad to keep field width/filename length a constant, use sprintf , optionally along with disp . 如果要显示更多控制精确格式的数字,例如控制要显示的小数点数或零填充以保持字段宽度/文件名长度不变,请使用sprintf ,可选择与disp一起使用。 For example: 例如:

sprintf('%0.5f',1/eps)          % 4503599627370496.00000
sprintf('%0.5g',1/eps)          % 4.5036e+15
sprintf('%05d.txt',1);          % 00001.txt
sprintf('%05d.txt',1000);       % 01000.txt

fprintf is like sprintf but for writing to file (the formatting commands are the same). fprintf就像sprintf但是用于写入文件(格式化命令是相同的)。 You're not actually changing the number, just the display, so these functions are useful if you want a function or script to display some sort of output in a human-readable fashion, while retaining the more accurate values for calculation: 您实际上并没有更改数字,只是显示,所以如果您希望函数或脚本以人类可读的方式显示某种输出,同时保留更准确的计算值,这些函数非常有用:

n = 5;
m = 50.53962983
disp(sprintf('Iteration %d: %2.2f',n, m)); % Iteration 5: 50.53
disp(m);  % 50.5396 - depends on your format setting
disp(sprintf('%0.8f',m)) % 50.53962983

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM