필터 지우기
필터 지우기

Decimal number between 0 and 1 without the integer part?

조회 수: 1 (최근 30일)
Rahul Pillai
Rahul Pillai 2017년 10월 4일
편집: Jan 2017년 10월 4일
Hi, I am trying to plot some outputs on a graph as text and I am basically plotting several points and need to conserve on space. Is there anyway I can plot a number between 0 and 1 , for eg, "0.25" as ".25" ignoring the 0? This is to be plotted as text() on the graph and so I'm using the num2str(x,y) function.
  댓글 수: 2
John BG
John BG 2017년 10월 4일
Hi Rahul
this is John BG jgb2012@sky.com
per isakson answer only takes 2 decimals, regardless of the actual amount of decimals.
Wouldn't it make more sense to first measure the amount of decimals and then generate a string with all decimals?
Jan
Jan 2017년 10월 4일
@John BG: What is the "actual amount of decimals"?
sprintf('%.17g', 0.1 + 0.1 + 0.1)
0.30000000000000004
Because most decimal number do not have an exact representation in the binary IEEE754 format, it would be required to round to a certain number of decimals before you can decide, how many decimals a number has. This will be more or less arbitrary.
If there is enough space for more digits, Rahul can simply use num2str(x, 3) or even omit the specification of the number of digits.

댓글을 달려면 로그인하십시오.

채택된 답변

per isakson
per isakson 2017년 10월 4일
This code strips off leading zeros; replaces leading zeros with an empty string.
>> str = regexprep( num2str( 0.25, 2 ), '^0*', '' )
str =
.25
>>
  댓글 수: 1
Jan
Jan 2017년 10월 4일
편집: Jan 2017년 10월 4일
Or:
strrep(sprintf('%.2f', 0.25), '0.', '.')

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by