如何在PHP中为数字添加逗号(会话值)

时间:2015-09-23 10:28:37

标签: php

我想改变这个:

4000

到这个

4,000

我正在使用此代码

<?php echo number_format(($_SESSION["result"]),",")  ; ?>

但它没有打印......

3 个答案:

答案 0 :(得分:0)

使用money_formatnumber_format

示例01

$number = 1234.56;

//setlocale(LC_MONETARY, 'en_US');
echo money_format('%i', $number) . "\n";
//output -  USD 1,234.56

示例02

 // number_format ( float $number , int $decimals = 0 , string $dec_point = "." , string $thousands_sep = "," );
number_format($number, 0, ".", ",");
//output -  1,234.56

答案 1 :(得分:0)

查看参数shown here

<?php echo number_format(($_SESSION["result"]), 3,",", "."); ?>

答案 2 :(得分:0)

试试这个它会给你印度格式的卢比

#include <windows.h> 
#include <stdlib.h> 
#include <stdio.h> 

#include <sql.h> 
#include <sqlext.h> 
#include<sqltypes.h>




#include<exception>
#include<iostream>
#include<stdio.h>


#include "Debug\sqlncli.h";
int main()
{
    SQLHENV henv=SQL_NULL_HENV;
    SQLHDBC hdbc=SQL_NULL_HDBC;
    SQLHSTMT hstmt=SQL_NULL_HSTMT;
    RETCODE rc;

   SQLWCHAR dsn[30] = L"mssqltest"; //Name DNS
   SQLWCHAR user[10] = L"di_test";
   SQLWCHAR pass[10] = L"di_test";

   SQLCHAR retValFName[256];
   SQLCHAR retValLName[256];
   SQLINTEGER cbLName,cbFName;
   SQLLEN cName=0;

   SQLCHAR *TVPTableName=(SQLCHAR *) "Person";


   SQLINTEGER Arraycode[10]={214,524,635,879,124,951,357,845,236,438};
   SQLCHAR Arrayname[10]={ 'P',  'C', 'A', 'G', 'd', 'S', 'R', 'U', 'Q'};

    SQLLEN cbTVPTableNAme,cbArraycode[10],cbArrayname[10];

   //This line is very important for working with TVP 
    // 10 is the number of values, which should be added to TVP
    rc=SQLBindParameter(hstmt,1,SQL_PARAM_INPUT,SQL_C_DEFAULT, SQL_SS_TABLE,10,0,TVPTableName,SQL_NTS,&cbTVPTableNAme);
    extract_error(" ",hstmt,SQL_HANDLE_STMT);
}

}

function makecomma($input)
{
if(strlen($input)<=2)
{ return $input; }
$length=substr($input,0,strlen($input)-2);
$formatted_input = makecomma($length).",".substr($input,-2);
return $formatted_input;
}
function formatInIndianStyle($num){
$pos = strpos((string)$num, ".");
if ($pos === false) { $decimalpart="00";}
else { $decimalpart= substr($num, $pos+1, 2); $num = substr($num,0,$pos); }

if(strlen($num)>3 & strlen($num) <= 12){
            $last3digits = substr($num, -3 );
            $numexceptlastdigits = substr($num, 0, -3 );
            $formatted = makecomma($numexceptlastdigits);
            $stringtoreturn = $formatted.",".$last3digits.".".$decimalpart ;
}elseif(strlen($num)<=3){
            $stringtoreturn = $num.".".$decimalpart ;
}elseif(strlen($num)>12){
            $stringtoreturn = number_format($num, 2);
}

if(substr($stringtoreturn,0,2)=="-,"){$stringtoreturn = "-".substr($stringtoreturn,2 );}
$stringtoreturn=str_replace(".00","",$stringtoreturn);
return $stringtoreturn;