View Raw SPL
/*****************************************************************************
*                                                                            *
*   NUM2STR.SPL  Copyright (C) 2010-2015 DSP Development Corporation         *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    converts a numer or series into a string                    *
*                                                                            *
*   Revisions:   16 Nov 2010  RRR  Creation                                  *
*                17 Sep 2015  RRR  String format check                       *
*                                                                            *
*****************************************************************************/

#if @HELP_NUM2STR

    NUM2STR

    Purpose: Converts a number or series to a string with optional precision.

    Syntax:  NUM2STR(val, format)

               val  - A real or series to convert to a string.

             format - Optional, an integer or string. If an integer, the
                      maximum number of significant digits. If a string,
                      the format string as specified by SPRINTF.

    Returns: A string.

    Example:
             str1 = "The value if PI is " + num2str(pi);
             str2 = "The value of PI is " + num2str(pi, 10);
             str3 = "The value of PI is " + num2str(pi, "%1.7f");

             str1 == "The value if PI is 3.14159"
             str2 == "The value of PI is 3.141592654"
             str3 == "The value of PI is 3.1415927"
    Remarks:
             NUM2STR uses the current default if format is unspecified.

             The actual number of significant digits depends on the
             magnitude of the input value.

             See SPRINTF for generic string formatting.
    See Also:
             Numstr
             Setformat
             Setprecision
             Sprintf 
             Strcat 
#endif


static tmpform = {};

/* convert a numeric into a string */
num2str(val, form)
{
        local prec, tmpform, str;

        if (argc < 2)
        {
                if (argc < 1) error("num2str - input required");

                /* check internal format */
                form = setformat();

                if (isstring(form))
                {
                        if (strlen(form) <= 0)
                        {
                                form = -1;
                        }
                }
        }

        if (isscalar(form))
        {
                if (form < 0)
                {
                        /* internal default */
                        str = evaltostr("val");
                }
                else
                {
                        /* use defined precision */

                        /* current format */
                        tmpform = num2str_getformat();

                        /* %1.Xg */
                        setformat(sprintf("%%1.%dg", castint(form)));

                        str = evaltostr("val");

                        /* reset */
                        setformat(tmpform);
                        tmpform = {};
                }
        }
        else if (isstring(form))
        {
                /* use user format string */
                tmpform = num2str_getformat();
                setformat(form);

                str = evaltostr("val");

                setformat(tmpform);
                tmpform = {};
        }
        else
        {
                /* use current defaults */
                str = evaltostr("val");
        }

        return(str);
}


/* current format */
num2str_getformat()
{
        local form;

        form = setformat();

        if (isstring(form))
        {
                if (strlen(form) <= 0)
                {
                        form = -1;
                }
        }
        
        return(form);
}


/* error handler */
num2str_error(errnum)
{
        if (not(isempty(tmpfmt)))
        {
                setformat(tmpfmt);
                tmpfmt = {};
        }

        error(errnum);
}