Converting a floating point number to string - matlab

I want to convert a floating point number to string.
f=0.0000
str=num2str(f,4);
This give me this-
str=
0
But I wanted to keep the result till 4 decimal points.I understand using num2str(f,4) results in precision of maximum 4 significant digits after decimal.It is not equal to 4 but maximum and that is why I get this answer.I want to know is there any way to convert this number to string with exactly 4 significant digits after decimal point?

Try using sprintf instead:
str = sprintf('%.4f', f)

Related

How To Only Show Certain Number of numbers after decimal point Double Swift

I am using doubles to log hours for my app. My problem is I only want to record the number and the first number after the decimal place otherwise sometimes the number becomes like this (x.9000000000001) and I only need the x.9.
I have tried rounding the double value but it still has this weird extra amount of zeros.
Any way to only get the double to show the first number after decimal place.
Thanks
The easiest way to achieve rounding to the first decimal place is to simply do the following:
let x = 4.9000000001
let roundedX = Double(round(x * 10) / 10) // roundedX = 4.9
roundedX will be a Double representing x rounded to the first decimal. To get 2 decimal places, just multiply and divide by 100 instead of 10.

MATLAB's num2str is inconsistent

I want to receive the string representation of a number, 2 points after the dot.
I'm using MATLAB R2015a, and noticed that num2str function returns inconsistent results:
for 0.511 I get the required result (0.51):
num2str(0.511,2)
ans =
0.51
for 1.711 I get 1.7 instead of 1.71:
num2str(1.511,2)
ans =
1.5
Anyone knows why?
the 'precision' scalar input to num2str is the number of significant digits. if you want to have 2 figures after the decimal point use the 'formatSpec' string argument:
num2str(0.511,'%.2f')
0.51
num2str(1.511,'%.2f')
1.51
From the documentation for num2str():
s = num2str(A,precision) returns a character array that represents the numbers with the maximum number of significant digits specified by precision.
In other words, the second parameter controls the total number of significant figures, not the number of decimal places.
If you want to round to two decimal places, then use the round() function. You can try rounding the number before calling num2str():
num2str(round(1.511,2))

Number of decimal digits to show

How to change the number of decimal digits?
Changing the format Matlab can show only 4 (if short) or 15 (if long). But I want exactly 3 digits to show.
To elaborate on Hamataro's answer, you could also use roundn function to round to a specific decimal precision, e.g.: roundn(1.23456789,-3) will yield 1.235. However, Matlab will still display the result in either of the formats you have mentioned, i.e 1.2350 if format is set to short, and 1.235000000000000 if format is set for long.
Alternatively, if you use sprintf, you can use the %g formatting option to display only a set number of digits, regardless of where the decimal point is. sprintf('%0.3g',1.23456789) yields 1.23; sprintf('%0.3g',12.3456789) yields 12.3
You can either use sprintf or do *
var2 = round(var1*1000)/1000

How to stop matlab truncating long numbers

These two long numbers are the same except for the last digit.
test = [];
test(1) = 33777100285870080;
test(2) = 33777100285870082;
but the last digit is lost when the numbers are put in the array:
unique(test)
ans = 3.3777e+16
How can I prevent this? The numbers are ID codes and losing the last digit is screwing everything up.
Matlab uses 64-bit floating point representation by default for numbers. Those have a base-10 16-digit precision (more or less) and your numbers seem to exceed that.
Use something like uint64 to store your numbers:
> test = [uint64(33777100285870080); uint64(33777100285870082)];
> disp(test(1));
33777100285870080
> disp(test(2));
33777100285870082
This is really a rounding error, not a display error. To get the correct strings for output purposes, use int2str, because, again, num2str uses a 64-bit floating point representation, and that has rounding errors in this case.
To add more explanation to #rubenvb's solution, your values are greater than flintmax for IEEE 754 double precision floating-point, i.e, greater than 2^53. After this point not all integers can be exactly represented as doubles. See also this related question.

how to remove last zero from number in matlab

If I set a variable in Matlab, say var1 = 2.111, after running the script, Matlab returns var1 = 2.1110. I want Matlab to return the original number, with no trailing zero. Anyone know how to do this. Thanks in advance.
By default Matlab displays results in Short fixed decimal format, with 4 digits after the decimal point.
You can change that to various other format such as:
long
Long fixed decimal format, with 15 digits after the decimal point for double values, and 7 digits after the decimal point for single values.
3.141592653589793
shortE
Short scientific notation, with 4 digits after the decimal point.
Integer-valued floating-point numbers with a maximum of 9 digits do not display in scientific notation.
3.1416e+00
longE
Long scientific notation, with 15 digits after the decimal point for double values, and 7 digits after the decimal point for single values.
Integer-valued floating-point numbers with a maximum of 9 digits do not display in scientific notation.
3.141592653589793e+00
shortG
The more compact of short fixed decimal or scientific notation, with 5 digits.
3.1416
longG
The more compact of long fixed decimal or scientific notation, with 15 digits for double values, and 7 digits for single values.
3.14159265358979
shortEng
Short engineering notation, with 4 digits after the decimal point, and an exponent that is a multiple of 3.
3.1416e+000
longEng
Long engineering notation, with 15 significant digits, and an exponent that is a multiple of 3.
3.14159265358979e+000
However I don't think other options are available. If you absolutely want to remove those zeros you would have to cast you result in a string and remove the trailing 0 characters and then display your result as a string and not a number.