css format text currency - ContentEssaywritercsClub

css format text currency

w3schools.com

THE WORLD’S LARGEST WEB DEVELOPER SITE

HTML
CSS
JAVASCRIPT
SQL
PHP
BOOTSTRAP
HOW TO
JQUERY
W3.CSS
PYTHON
XML
MORE


FORUM
EXAMPLES
REFERENCES

×

PHP Tutorial

PHP HOME
PHP Intro
PHP Install
PHP Syntax
PHP Variables
PHP Echo / Print
PHP Data Types
PHP Strings
PHP Constants
PHP Operators
PHP If…Else…Elseif
PHP Switch
PHP While Loops
PHP For Loops
PHP Functions
PHP Arrays
PHP Sorting Arrays
PHP Superglobals

PHP Forms

PHP Form Handling
PHP Form Validation
PHP Form Required
PHP Form URL/E-mail
PHP Form Complete

PHP Advanced

PHP Arrays Multi
PHP Date and Time
PHP Include
PHP File Handling
PHP File Open/Read
PHP File Create/Write
PHP File Upload
PHP Cookies
PHP Sessions
PHP Filters
PHP Filters Advanced
PHP Error Handling
PHP Exception

MySQL Database

MySQL Database
MySQL Connect
MySQL Create DB
MySQL Create Table
MySQL Insert Data
MySQL Get Last ID
MySQL Insert Multiple
MySQL Prepared
MySQL Select Data
MySQL Delete Data
MySQL Update Data
MySQL Limit Data

PHP – XML

PHP XML Parsers
PHP SimpleXML Parser
PHP SimpleXML – Get
PHP XML Expat
PHP XML DOM

PHP – AJAX

AJAX Intro
AJAX PHP
AJAX Database
AJAX XML
AJAX Live Search
AJAX RSS Reader
AJAX Poll

PHP Examples

PHP Examples
PHP Quiz
PHP Certificate

PHP Reference

PHP Overview
PHP Array
PHP Calendar
PHP Date
PHP Directory
PHP Error
PHP Filesystem
PHP Filter
PHP FTP
PHP HTTP
PHP Libxml
PHP Mail
PHP Math
PHP Misc
PHP MySQLi
PHP SimpleXML
PHP String
PHP XML
PHP Zip
PHP Timezones

PHP money_format() Function

❮ PHP String Reference

Example

International en_US format:

<?php
$number = 1234.56;
setlocale(LC_MONETARY,"en_US");
echo money_format("The price is %i", $number);
?>

The output of the code above will be:

The price is USD 1,234.56


Definition and Usage

The money_format() function returns a string formatted as a currency string.

This function inserts a formatted number where there is a percent (%)
sign in the main string.

Note: The money_format() function does not work on Windows platforms.

Tip: This function is often used together with the
setlocale() function.

Tip: To view all available language codes, go to our Language code reference.


Syntax

money_format(string,number)

ParameterDescription
stringRequired. Specifies the string to be formatted and how to format the variables in it.

Possible format values:

Padding and Flags:

  • =f – Specifies the character (f) to be used as padding (Example: %=t this uses "t" as padding). Default is space
  • ^ – Removes the use of grouping characters
  • + or ( – Specifies how to show positive and negative numbers. If "+" is used, the local setting for + and – will be used (usually a sign in front of negative numbers, and nothing in front of positive numbers). If "(" is used, negative numbers are enclosed in parenthesis. Default is "+"
  • ! – Stops the use of currency symbols in the output string
  • – If "-" is used, all fields are left-justified. Default is right-justified

Field width:

  • x – Specifies the minimum field width (x). Default is 0
  • #x – Specifies the maximum number (x) of digits expected to the left of the decimal point. This is used to keep formatted output aligned in the same columns. If the number of digits are bigger than x, this specification is ignored
  • .x – Specifies the maximum number (x) of digits expected to the right of the decimal point. If x is 0, the decimal point and the digits to its right will not be shown. Default is local settings

Conversion characters:

  • i – The number is formatted to international currency format
  • n – The number is formatted to national currency format
  • % – Returns the % character

Note: If multiple format values are used, they must be in the same order as shown above.

Note: This function is affected by local settings.

numberRequired. The number to be inserted at the %-sign in the format string


Technical Details

Return Value:Returns the formatted string. Characters before and after the formatting string will be returned unchanged. Non-numeric number causes returning NULL and emitting E_WARNING
PHP Version:4.3.0+

More Examples

Example 1

International format (Germany) with 2 decimals:

<?php
$number = 1234.56;
setlocale(LC_MONETARY,"de_DE");
echo money_format("%.2n", $number);
?>

The output of the code above will be:

1 234,56 EUR

Example 2

Negative number, US national format with () to indicate negative numbers and
2 digits of right precision and "*" as a fill character:

<?php
$number = -1234.5672;
echo money_format("%=*(#10.2n",$number);
?>

The output of the code above will be:

(******1234.57)


❮ PHP String Reference

COLOR PICKER

colorpicker

HOW TO

Tabs
Dropdowns
Accordions
Convert Weights
Animated Buttons
Side Navigation
Top Navigation
Modal Boxes
Progress Bars
Parallax
Login Form
HTML Includes
Google Maps
Range Sliders
Tooltips
Slideshow
Filter List
Sort List

SHARE

CERTIFICATES

HTML ,
CSS ,
JavaScript ,
PHP ,
jQuery ,
Bootstrap and
XML .

Read More »



REPORT ERROR
PRINT PAGE
FORUM
ABOUT

×

Your Suggestion:

×

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top 10 Tutorials

HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
W3.CSS Tutorial
Bootstrap Tutorial
SQL Tutorial
PHP Tutorial
jQuery Tutorial
Python Tutorial

Top 10 References

HTML Reference
CSS Reference
JavaScript Reference
W3.CSS Reference
Bootstrap Reference
SQL Reference
PHP Reference
HTML Colors
jQuery Reference
Python Reference

Top 10 Examples

HTML Examples
CSS Examples
JavaScript Examples
How To Examples
W3.CSS Examples
Bootstrap Examples
PHP Examples
jQuery Examples
Angular Examples
XML Examples

Web Certificates

HTML Certificate
CSS Certificate
JavaScript Certificate
jQuery Certificate
PHP Certificate
Bootstrap Certificate
XML Certificate


W3Schools is optimized for learning, testing, and training. Examples might be simplified to improve reading and basic understanding.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content.
While using this site, you agree to have read and accepted our terms of use ,
cookie and privacy policy .
Copyright 1999-2018 by Refsnes Data. All Rights Reserved.
Powered by W3.CSS .

W3Schools.com

Stack Overflow



  1. Log In
    Sign Up

  2. current community


    • Stack Overflow

      help
      chat

    • Meta Stack Overflow

    your communities

    Sign up or log in to customize your list.

    more stack exchange communities

    company blog

    • Tour

      Start here for a quick overview of the site

    • Help Center

      Detailed answers to any questions you might have

    • Meta

      Discuss the workings and policies of this site

    • About Us

      Learn more about Stack Overflow the company

    • Business

      Learn more about hiring developers or posting ads with us

This site uses cookies to deliver our services and to show you relevant ads and job listings.
By using our site, you acknowledge that you have read and understand our Cookie Policy , Privacy Policy , and our Terms of Service .
Your use of Stack Overflow’s Products and Services, including the Stack Overflow Network, is subject to these policies and terms.

Join us in building a kind, collaborative learning community via our updated
Code of Conduct .

Join Stack Overflow to learn, share knowledge, and build your career.

Email Sign Up
Sign Up

or sign in with


Google

Facebook

Formatting a number as currency using CSS

Ask Question


up vote
27
down vote

favorite

4

Just wondering if anyone knows whether it is possible to format the content of an element as currency using only CSS. It would be nice to have how the value is presented in CSS if possible, can’t find anything though so I’m not holding my breath 🙂

<!DOCTYPE html>
<html>
<head> <style type="text/css"> .dollars:before content:'$'; </style>
</head>
<body> Pure CSS: <span >25153.3</span> <br /> Ideal format: <span>$25,153.30</span>
</body>
</html>

That example comes out as:

Pure CSS: $25153.3

Ideal format: $25,153.30

Also I’m aware that it’s fairly trivial using javascript – http://css-tricks.com/snippets/javascript/format-currency/ .

html css format
share | improve this question

asked Feb 21 ’12 at 5:27

Daniel Imms

32.1k493130

  • Nope. Not with pure css.
    –  Scott
    Feb 21 ’12 at 5:29

  • possible duplicate of stackoverflow.com/questions/8677805/…
    –  BoltClock
    Feb 21 ’12 at 5:34

add a comment  | 

3 Answers
3

active

oldest

votes


up vote
12
down vote

The currency format can be achieved with CSS and a bit of Javascript which is needed for the parsing of the number to add the commas. A CSS class adds the additional styling like negative (red) or currency sign (i.e. $ Dollar sign). The approach is a follows:

1) Convert the value to number (adds the commas based on the locale)

Number(value).toLocaleString('en');

2) Add a class to determine if it is negative or positive value (i.e. red color)

.enMoney::before content:"$";
.negMoney color:red;

See more detail here with the sample code and css:

http://www.ozkary.com/2014/04/format-currency-with-javascript-and-css.html

share | improve this answer

edited Jan 17 ’17 at 17:33

Willi Mentzel

6,988113863

answered Dec 12 ’15 at 15:46

ozkary

1,2551419

  • Can’t affect the middle thousand sepparators, but before and after a number is enough for the goal…
    –  DavidTaubmann
    Dec 17 ’16 at 23:37

add a comment  | 


up vote
3
down vote

If you’re asking about number formatting in CSS (that is, parsing a number from a string and then formatting it with thousands separator, decimal separator, fixed decimal digits number etc), then no, it is impossible in CSS, and this is not what CSS was designed for.

If you want to do any formatting, then you’d better to use XSLT. For example:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="span[@class='dollars']"> <span> <xsl:text>$</xsl:text> <xsl:value-of select="format-number(current(), '###,###.00')"/> </span> </xsl:template> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template>
</xsl:stylesheet>

share | improve this answer

edited Feb 21 ’12 at 5:37

answered Feb 21 ’12 at 5:29

penartur

7,76642645

  • 59

    "and this is not what CSS was designed for"… I would reserve judgement about what CSS was designed for. Pushing common functionality into the browser is exactly what technologies like CSS were designed for. There was a time when "responding to screen sizes" was "not something CSS was designed for" Also: wiki.csswg.org/ideas/content-formatting
    –  umassthrower
    Mar 19 ’13 at 22:41

  • 4

    aha – so that’s what XSLT was designed for 😉
    –  Simon_Weaver
    Feb 10 ’15 at 9:21

  • 14

    "Formatting a number" is a function of presentation, and that’s exactly what CSS was designed for.
    –  Aaron Cicali
    Feb 12 ’16 at 17:37

add a comment  | 


up vote
0
down vote

Well, this doesn’t fit your own use case unfortunately, and it’s fairly obvious, but you could do this for integer numbers from -999 to 999.

.currency:before content:'$';
.currency:after content: '.00'; 
<span >789</span>

Then, a possible, annoying, solution if you’re working server side, is to convert your number to a reversed string and loop over each character. If you really wanted you could place the characters into li’s and css could then do the formatting you wanted as follows. However this is incredibly pointless as you may as well simply author the string at that point.

.currency li:before content: ' ,';
.currency li:first-child:before content:'$' !important;
.currency * display: inline-block;
.currency, .currency > * display: inline-block;
.currency:after content: '.00'; 
<ul ><li>123</li><li>456</li><li>789</li></ul>
<ul ><li>456</li><li>789</li></ul>
<ul ><li>789</li></ul> 

For an even deeper dive into pointless effort, you could prepend a

<style> .currency:after content: '.00'; </style>

above the <ul>, allowing your script to change the decimal value in CSS, lol

Still, if you were to cave and use JavaScript, then the CSS may actually be somewhat useful. You can output a plain int or double (any precision) and just have JS break it into <li>s.

share | improve this answer

edited Nov 2 ’17 at 3:50

answered Nov 2 ’17 at 3:41

Garet Claborn

70521240

add a comment  | 

Not the answer you’re looking for? Browse other questions tagged html css format or ask your own question .

asked

6 years, 6 months ago

viewed

119,533 times

active

9 months ago

Linked

96

Formatting numbers (decimal places, thousands separators, etc) with CSS

9

How do I display large numbers with commas? HTML

0

Format the display of a field

Related

2934

Set cellpadding and cellspacing in CSS?

1398

Convert HTML + CSS to PDF with PHP?

2003

How do I give text or an image a transparent background using CSS?

2528

Is there a CSS parent selector?

1980

When to use margin vs padding in CSS

3610

Change an HTML5 input’s placeholder color with CSS

1611

How can I transition height: 0; to height: auto; using CSS?

1644

How do CSS triangles work?

1826

How do I vertically center text with CSS?

2443

Is it possible to apply CSS to half of a character?

Hot Network Questions

  • non parametric alternative to simple t-test

  • Can you switch which favored class bonus you take on each level up?

  • ssh "-P" option, what’s it used for?

  • Is it possible for an ancient contraption to survive modern-day attempts of breaking in?

  • A magician can draw magical power from other planets in a star system, but not their own – why?

  • Can you sneak into Premium Economy Class?

  • Tanach verses with narrative similar to Quran 2:61

  • Listing a Stack Overflow user as co-author for having provided substantial programming support

  • How to print multiline variables in side-by-side columns (bash)?

  • How to rotate scene via python script?

  • How do I proceed to seriously study Buddhism?

  • Inductive and deductive arguments and mathematical induction

  • Range of a rational function with radicals

  • Bookshelf Arrangement Dilemma

  • Should I tilt my DIY self-standing bookshelf backwards to prevent it falling down?

  • Where did Dumbledore live while working at Hogwarts?

  • What’s the pyramid joke?

  • Why is storing passwords in version control a bad idea?

  • Local char* – keeps its value

  • Why do airliners have to park so accurately?

  • TikZ: centering nodes and paths relative to each other

  • Why were floppy disks invented after hard disks?

  • How to solve this kind of nonlinear differential equation?

  • What are the mechanical and role-playing advantages of playing a human?

more hot questions


question feed

default

Stack Overflow works best with JavaScript enabled