Determining arbitrary precision from text

1 view (last 30 days)
I am reading a number with arbitray precision, but I need to parse this into a string using a particular format.
Example 1: 1.015
Example 2: 60.15
Example 3: 700.150
In the first example, the correct string is CASE1_15. In the second example, the correct string is CASE60_15. In the last example, the correct string is CASE700_150
The code below accomplishes this, where the number has been stored in the fullcase column of matrix a, assuming a precision of 3 decimal places. Unfortunately, this requires me to know the precision beforehand. This means the code works for Examples 1 and 3, but fails for Example 2. If I alter code(replacing 1000 with 100), it fails for Examples 1 and 3, but works for Example 2. Isn't there some elegant means to detect the precision stored in that column so the code works no matter what the precision is?
strCase = num2str(floor(a(j,fullcase)));
strSubcase = num2str((a(j,fullcase)-floor(a(j,fullcase)))*1000);
fullCase = ['CASE',strCase,'_',strSubcase];

Accepted Answer

Walter Roberson
Walter Roberson on 20 Jan 2012
Once you have converted to numeric form, you cannot determine the precision -- and the number actually stored probably isn't what you are expecting. Please see http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!