Number format

Discussion in 'PHP' started by emi87, Dec 16, 2010.

  1. #1
    Hello,

    1. I don't know how to create an input that allows me to write a number format in this 3 ways:
    a) xxxxx(any) ===> the resulted no. will be xxxxx.00
    b) xxxx.x ===> the resulted no. will be xxxx.x0
    c) xxxxxxxxxxx(any).xx(max 2) ===> the resulted no. will be xxxxxxxxxx.xx
    NOT allowing me to vrite xxx,xxx.xx with coma only simple or with one dot and if dot is written in input just 1 or 2 numbers after dot to be allowed.

    For example If I write $11 111 I want to be inserted into myqsl table as:
    a) if I write 11111 to be inserted as 11,111.00
    b) if I write 11111.1 to be inserted as 11,111.10
    c) if I write 11111.11 to be inserted as 11,111.11
    AND NOT allowing me to write 11,111 or 11,111.11 or 11.111,11 or others (with coma)

    2. What field must be in mysql table to be compatible to add 2 or more rows like:
    id | price
    1 | 1,111.00
    2 | 111,111.11

    Please write a code with which I would be able to insert these number formats and after that to select them from a mysql table and add them or substract them.

    THank you very much.
    I really need help.
     
    emi87, Dec 16, 2010 IP
  2. shofstetter

    shofstetter Well-Known Member

    Messages:
    178
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    120
    #2
    Your best bet would be to use a field of the Float or double type in your MySql table. Both php and MySQL have functions for formatting output. In this have your table would store values without commas like this 111111.00 your can specify the precision (number of digits after decimal point) when you create the field in the table like this float(7,2) your stored values will look like this: 11111.11
    Explanation float(number of digits,number of digits after decimal point).
    When you store your values this way you can use php or mysql to perform math and other numerical operations before you display your results. Your results can be formated with php's format functions to get it to display the way you want it to ex:

    string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )
    string money_format ( string $format , float $number )

    reference to both functions can be found at:
    http://php.net/manual/en/function.number-format.php
    http://www.php.net/manual/en/function.money-format.php
     
    shofstetter, Dec 16, 2010 IP
  3. emi87

    emi87 Peon

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    shofstetter God bless you! THank you very much. This is what I needed and solved my issue:
    setlocale(LC_MONETARY, 'en_US');
    echo money_format('%i', $number) . "\n";
    // USD 1,234.56
    I fount it in your link. Thank you again >:D<
     
    emi87, Dec 16, 2010 IP
  4. emi87

    emi87 Peon

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Now my format is:
    - for positive USD 1,234.56 for example
    - for negative -USD 1,234.56
    I want just not showing me the USD currency .. just 1,234.56 or -1,234.56
    I have tried

    $number1 = 1200.56;
    $number2 = 34;
    $number = $number2+$number1;
    setlocale(LC_MONETARY, 'en_US');
    $mo= money_format('%i', $number) . "\n";
    $rest=substr($mo, 4);
    echo $rest;
    PHP:
    BUT
    for positive will output 1,234.56 => GOOD
    for negative will output olso 1,234.56 NOT -1,234.56 => BAD (if number=number2-number1)
     
    emi87, Dec 17, 2010 IP
  5. emi87

    emi87 Peon

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    SOLVED
    modified setlocale(LC_MONETARY, 'en_US');
    to setlocale(LC_MONETARY, 'en');
     
    emi87, Dec 17, 2010 IP