Jump to content

How to display formatted number in PHP?

- - - - -

  • Please log in to reply
5 replies to this topic

#1
newphpcoder

newphpcoder

    Programming Professional

  • Members
  • PipPipPipPipPipPip
  • 479 posts
Good day!

I want to ask if how does my Amount output has a ',' semicolon.

Here is my code:


$Amount = round(($Hours/8)* $Rate, 2); 


the output of this code is: 3884.34

I want the output is : 3,884.34

Thank you

#2
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
Then it would no longer be a number; number variables can't have non-number characters (ie comma (,)) in them, because of the way they are stored. You'll have to use a string for that kind of representation.

You can write a function that would convert the number to a string, and then insert the commas in the appropriate positions; implode() and explode() are some functions that you might find useful for this. You don't have to use implode() and explode(), but it's just an idea.

#3
newphpcoder

newphpcoder

    Programming Professional

  • Members
  • PipPipPipPipPipPip
  • 479 posts
Thank you...

#4
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
I think there should be functions that helps you show numbers with thousands-divider and stuff, I just don't remember which ones. They will also allow showing comma as decimal separator as some countries (as Sweden) uses (which don't use thousands-dividers instead)

edit!
I found it, it's PHP: number_format - Manual !

usage:

$Amount = round(($Hours/8)* $Rate, 2); 
echo number_format($Amount, 2, ".", ",");

__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#5
newphpcoder

newphpcoder

    Programming Professional

  • Members
  • PipPipPipPipPipPip
  • 479 posts
Thank you

#6
mahesh

mahesh

    Newbie

  • Members
  • Pip
  • 1 posts
Try number_format($value, 2, ".", ",");




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users