(self, value)
return valstr
Format a number to a format string.
Format a number to a format string.
Format a number to a format string .
def format(self, value):
        """Format a number to a format string."""
        value = values.pass_number(value)
        tokens = self._tokens
        digits_before = self._digits_before
        decimals = self._decimals
        comma = self._comma
        # promote ints to single
        value = value.to_float()
        # illegal function call if too many digits
        if digits_before + decimals > 24:
            raise error.BASICError(error.IFC)
        # dollar sign, decimal point
        has_dollar, force_dot = b'$' in tokens, b'.' in tokens
        # leading sign, if any
        valstr, post_sign = b'', b''
        neg = value.is_negative()
        if tokens[:1] == b'+':
            valstr += b'-' if neg else b'+'
        elif tokens[-1:] == b'+':
            post_sign = b'-' if neg else b'+'
        elif tokens[-1:] == b'-':
            post_sign = b'-' if neg else b' '
        else:
            valstr += b'-' if neg else b''
            # reserve space for sign in scientific notation by taking away a digit position
            if not has_dollar:
                digits_before -= 1
                if digits_before < 0:
                    digits_before = 0
        # take absolute value
        # NOTE: this could overflow for Integer -32768
        # but we convert to Float before calling format_number
        value = value.clone().iabs()
        # currency sign, if any
        valstr += b'$' if has_dollar else b''
        # format to string
        if b'^' in tokens:
            valstr += value.to_str_scientific(digits_before, decimals, force_dot, comma)
        else:
            valstr += value.to_str_fixed(decimals, force_dot, comma)
        # trailing signs, if any
        valstr += post_sign
        # add leading zero before radix if there's space
        if len(valstr) < len(tokens):
            if valstr.startswith(b'.'):
                valstr = b'0' + valstr
            elif valstr.startswith(b'+.'):
                valstr = b'+0' + valstr[1:]
            elif valstr.startswith(b'-.'):
                valstr = b'-0' + valstr[1:]
        if len(valstr) > len(tokens):
            # number does not fit in field
            valstr = b'%' + valstr
        else:
            # filler
            valstr = valstr.rjust(len(tokens), b'*' if b'*' in tokens else b' ')
        return valstr
def format ( self , value ) : value = values . pass_number ( value ) tokens = self . _tokens digits_before = self . _digits_before decimals = self . _decimals comma = self . _comma # promote ints to single value = value . to_float ( ) # illegal function call if too many digits if digits_before + decimals > 24 : raise error . BASICError ( error . IFC ) # dollar sign, decimal point has_dollar , force_dot = b'$' in tokens , b'.' in tokens # leading sign, if any valstr , post_sign = b'' , b'' neg = value . is_negative ( ) if tokens [ : 1 ] == b'+' : valstr += b'-' if neg else b'+' elif tokens [ - 1 : ] == b'+' : post_sign = b'-' if neg else b'+' elif tokens [ - 1 : ] == b'-' : post_sign = b'-' if neg else b' ' else : valstr += b'-' if neg else b'' # reserve space for sign in scientific notation by taking away a digit position if not has_dollar : digits_before -= 1 if digits_before < 0 : digits_before = 0 # take absolute value # NOTE: this could overflow for Integer -32768 # but we convert to Float before calling format_number value = value . clone ( ) . iabs ( ) # currency sign, if any valstr += b'$' if has_dollar else b'' # format to string if b'^' in tokens : valstr += value . to_str_scientific ( digits_before , decimals , force_dot , comma ) else : valstr += value . to_str_fixed ( decimals , force_dot , comma ) # trailing signs, if any valstr += post_sign # add leading zero before radix if there's space if len ( valstr ) < len ( tokens ) : if valstr . startswith ( b'.' ) : valstr = b'0' + valstr elif valstr . startswith ( b'+.' ) : valstr = b'+0' + valstr [ 1 : ] elif valstr . startswith ( b'-.' ) : valstr = b'-0' + valstr [ 1 : ] if len ( valstr ) > len ( tokens ) : # number does not fit in field valstr = b'%' + valstr else : # filler valstr = valstr . rjust ( len ( tokens ) , b'*' if b'*' in tokens else b' ' ) return valstr