Syntax
h=digitofnum(n,d)
Input Parameter
n : | | number |
d : | | digit of number |
Output Parameter
Examples
o=567.891
digitofnum(o,-2)
digitofnum(o,+2)
Copyright 2019-2025 Tim C. Lueth. All rights reserved. The code is the property of Tim C. Lueth and may not be redistributed or modified without explicit written permission. This software may be used free of charge for academic research and teaching purposes only. Commercial use, redistribution, modification, or reverse engineering is strictly prohibited. Access to source code is restricted and granted only under specific agreements. For licensing inquiries or commercial use, please contact: Tim C. Lueth
Algorithm (Workflow)
This function, digitofnum
, is designed to extract a specific digit from a given number. It is part of the SolidGeometry library and was introduced in version 4.5. The function uses a similar rule for digit extraction as the roundn
function.
Input Parameters
- n: The number from which a digit is to be extracted. It can be an integer or a floating-point number.
- d: The position of the digit to be extracted. The position is defined relative to the decimal point:
d = -1
: Extracts the digit in the tens place (10^1).
d = 0
: Extracts the digit in the units place (10^0).
d = 1
: Extracts the digit in the tenths place (10^-1).
Output
- z: The value of the digit at the specified position. If the position is out of range, the function returns 0.
Algorithm Steps
- Convert the number
n
to a string c
using num2str
.
- Find the position of the decimal point in
c
using strfind
. If there is no decimal point, append one to the end of the string.
- If
d
is less than or equal to 0, decrement d
by 1 to adjust for the position relative to the decimal point.
- Calculate the absolute position of the digit by adding
d
to the position of the decimal point.
- Check if the calculated position is valid:
- If the position is out of range (less than 0, equal to the position of the decimal point, or greater than the length of the string), set
z
to 0.
- Otherwise, convert the character at the calculated position to a number and assign it to
z
.
- If no output argument is specified, display the string
c
.
Example Usage
To extract the digit at the hundredths place from the number 567.891, use:
digitofnum(567.891, -2)
To extract the digit at the hundredths place from the number 567.891, use:
digitofnum(567.891, +2)
Algorithm explaination created using ChatGPT on 2025-08-18 23:55. (Please note: No guarantee for the correctness of this explanation)
Last html export of this page out of FM database by TL: 2025-09-21