digitofnum

by Tim C. Lueth, SG-Lib Toolbox: SolidGeometry 5.6 - Auxiliary function
Introduced first in SolidGeometry 4.5, Creation date: 2019-03-02, Last change: 2025-09-14

returns the digit at a specific position

Description

uses the same rule for digit as rounddiv
-1= 10^+1
0= 10^0
1= 10^-1

See Also:

Example Illustration

 missing image of digitofnum(n,d)

Syntax

h=digitofnum(n,d)

Input Parameter

n: number
d: digit of number

Output Parameter

h: digit value

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

Output

Algorithm Steps

  1. Convert the number n to a string c using num2str.
  2. 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.
  3. If d is less than or equal to 0, decrement d by 1 to adjust for the position relative to the decimal point.
  4. Calculate the absolute position of the digit by adding d to the position of the decimal point.
  5. 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.
  6. 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