# -*- coding: utf-8 mode: python -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8

__all__ = ('FloatF',)

import re

from ulib.base.base import isflt
from ulib.base.uio import _s, _u
from strings import UnicodeF

class FloatF(UnicodeF):
    RE_FLOAT = re.compile(r'(?:-)?\d+(?:\.\d+)?$')

    def parse(self, s):
        if isflt(s): return s

        s = UnicodeF.parse(self, s)
        if s is None: return None

        if self.RE_FLOAT.match(s) is None:
            raise ValueError("Invalid number: %s" % s)

        return float(s)

    def format(self, f):
        if f is None: return u""
        else: return _u(f)
