No BSD License  

Highlights from
chopDigit

3.5

3.5 | 4 ratings Rate this file 3 Downloads (last 30 days) File Size: 846 Bytes File ID: #5374

chopDigit

by Andrew Hastings

 

29 Jun 2004 (Updated 30 Jun 2004)

Remove unwanted digits from numbers.

| Watch this File

File Information
Description

This method provides a means to specify how many digits of precision to give to a number. For instnace if you have number which is 19.2 and another calculated number that is 19.2000000001 that are suppose to be equal you can just chop the digits for the 19.2000000001 to one digit of precision and then the two numbers will be equal.

MATLAB release MATLAB 6.1 (R12.1)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (6)
01 Jul 2004 Jos jnvdg @ arcor DOT de

There are numerous better and faster ways to do this!
e.g. fix(N * (10^P)) / (10^P) with N the number and P the precision.

02 Jul 2004 Charles Zane

It might not be the fastest or best way to do it. But it shows outside of the box thinking! "A" for effort. What with processors being upwards of 3.2 GHz I don't think speed is an issue.

25 Jul 2004 Toby Fernsler

Both ChopDigit.m nor Jos' suggested solution assume the data is of the form #.###. So for example if you want to round 582.34 to 4 significant digits (582.3), these approaches would instead yield 582.3400; 4 decimal places of precision. I have not found a decent solution, a comment in Support mentions the program CHOP, but I can't find it. The symbolic toolboxs can do this many ways, but they're too slow for my needs.

08 Oct 2005 Ranganathan Sridharan

Good Job! Exactly met my requirements.

25 Jun 2006 Andreas Zöllner

I would propose the following:
function result = significant(x, n)
% significant(x, n) rounds number x to n number of significant figures

s = floor(log10(abs(x)));
shift = 10^(n-1);
mant = round(x*shift/(10^s)) / shift;
result = mant * 10^s;

07 Aug 2007 Matthew McMahon

Not bad, but Andreas' proposal works much better for negative numbers.

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
round extra digits Andrew Hastings 22 Oct 2008 07:25:25

Contact us at files@mathworks.com