Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: convert double to string
Replies: 1   Last Post: Dec 29, 2013 10:00 AM

 Messages: [ Previous | Next ]
 Nasser Abbasi Posts: 6,677 Registered: 2/7/05
Re: convert double to string
Posted: Dec 29, 2013 10:00 AM

On 12/29/2013 5:04 AM, fatemeh wrote:
> I want convert double number like 0.222833 to string but when
>i using num2str the number convert to '0.22283' and sixth digit
>of number is removed ,can u help me to convert it to string?
>

just be carful, what you see in the screen is not always the
actual binary value of the number.

EDU>> n=rand(1,1)
n =
0.8147

EDU>> format long
EDU>> n
n =
0.814723686393179

EDU>> num2strexact(n)

0.81472368639317893634910205946653150022029876708984375

EDU>>

One way to convert to string: decide on the number of
digits you want, and you can use sprintf:

EDU>> s=sprintf('%3.10f',n)

0.8147236864

EDU>> s=sprintf('%3.16f',n)
0.8147236863931789

or you can use num2str second argument:

EDU>> num2str(n,10)
0.8147236864

EDU>> num2str(n,16)
0.8147236863931789

--Nasser

Date Subject Author
12/29/13 fatemeh
12/29/13 Nasser Abbasi