Php number format no rounding

The NumberFormatter class

Programs store and operate on numbers using a locale-independent binary representation. When displaying or printing a number it is converted to a locale-specific string. For example, the number 12345.67 is «12,345.67» in the US, «12 345,67» in France and «12.345,67» in Germany.

By invoking the methods provided by the NumberFormatter class, you can format numbers, currencies, and percentages according to the specified or default locale. NumberFormatter is locale-sensitive so you need to create a new NumberFormatter for each locale. NumberFormatter methods format primitive-type numbers, such as double and output the number as a locale-specific string.

In order to format percentages, create a locale-specific formatter with percentage format type. With this formatter, a decimal fraction such as 0.75 is displayed as 75%.

For more complex formatting, like spelled-out numbers, the rule-based number formatters are used.

Class synopsis

public parse ( string $string , int $type = NumberFormatter::TYPE_DOUBLE , int &$offset = null ): int | float | false

Predefined Constants

These styles are used by the numfmt_create() to define the type of the formatter. NumberFormatter::PATTERN_DECIMAL Decimal format defined by pattern NumberFormatter::DECIMAL Decimal format NumberFormatter::CURRENCY Currency format NumberFormatter::PERCENT Percent format NumberFormatter::SCIENTIFIC Scientific format NumberFormatter::SPELLOUT Spellout rule-based format NumberFormatter::ORDINAL Ordinal rule-based format NumberFormatter::DURATION Duration rule-based format NumberFormatter::PATTERN_RULEBASED Rule-based format defined by pattern NumberFormatter::CURRENCY_ACCOUNTING Currency format for accounting, e.g., ($3.00) for negative currency amount instead of -$3.00 . Available as of PHP 7.4.1 and ICU 53. NumberFormatter::DEFAULT_STYLE Default format for the locale NumberFormatter::IGNORE Alias for PATTERN_DECIMAL

These constants define how the numbers are parsed or formatted. They should be used as arguments to numfmt_format() and numfmt_parse() . NumberFormatter::TYPE_DEFAULT Derive the type from variable type NumberFormatter::TYPE_INT32 Format/parse as 32-bit integer NumberFormatter::TYPE_INT64 Format/parse as 64-bit integer NumberFormatter::TYPE_DOUBLE Format/parse as floating point value NumberFormatter::TYPE_CURRENCY Format/parse as currency value

Number format attribute used by numfmt_get_attribute() and numfmt_set_attribute() . NumberFormatter::PARSE_INT_ONLY Parse integers only. NumberFormatter::GROUPING_USED Use grouping separator. NumberFormatter::DECIMAL_ALWAYS_SHOWN Always show decimal point. NumberFormatter::MAX_INTEGER_DIGITS Maximum integer digits. NumberFormatter::MIN_INTEGER_DIGITS Minimum integer digits. NumberFormatter::INTEGER_DIGITS Integer digits. NumberFormatter::MAX_FRACTION_DIGITS Maximum fraction digits. NumberFormatter::MIN_FRACTION_DIGITS Minimum fraction digits. NumberFormatter::FRACTION_DIGITS Fraction digits. NumberFormatter::MULTIPLIER Multiplier. NumberFormatter::GROUPING_SIZE Grouping size. NumberFormatter::ROUNDING_MODE Rounding Mode. NumberFormatter::ROUNDING_INCREMENT Rounding increment. NumberFormatter::FORMAT_WIDTH The width to which the output of format() is padded. NumberFormatter::PADDING_POSITION The position at which padding will take place. See pad position constants for possible argument values. NumberFormatter::SECONDARY_GROUPING_SIZE Secondary grouping size. NumberFormatter::SIGNIFICANT_DIGITS_USED Use significant digits. NumberFormatter::MIN_SIGNIFICANT_DIGITS Minimum significant digits. NumberFormatter::MAX_SIGNIFICANT_DIGITS Maximum significant digits. NumberFormatter::LENIENT_PARSE Lenient parse mode used by rule-based formats.

Читайте также:  Все виды меню html

Number format text attribute used by numfmt_get_text_attribute() and numfmt_set_text_attribute() . NumberFormatter::POSITIVE_PREFIX Positive prefix. NumberFormatter::POSITIVE_SUFFIX Positive suffix. NumberFormatter::NEGATIVE_PREFIX Negative prefix. NumberFormatter::NEGATIVE_SUFFIX Negative suffix. NumberFormatter::PADDING_CHARACTER The character used to pad to the format width. NumberFormatter::CURRENCY_CODE The ISO currency code. NumberFormatter::DEFAULT_RULESET The default rule set. This is only available with rule-based formatters. NumberFormatter::PUBLIC_RULESETS The public rule sets. This is only available with rule-based formatters. This is a read-only attribute. The public rulesets are returned as a single string, with each ruleset name delimited by ‘;’ (semicolon).

Number format symbols used by numfmt_get_symbol() and numfmt_set_symbol() . NumberFormatter::DECIMAL_SEPARATOR_SYMBOL The decimal separator. NumberFormatter::GROUPING_SEPARATOR_SYMBOL The grouping separator. NumberFormatter::PATTERN_SEPARATOR_SYMBOL The pattern separator. NumberFormatter::PERCENT_SYMBOL The percent sign. NumberFormatter::ZERO_DIGIT_SYMBOL Zero. NumberFormatter::DIGIT_SYMBOL Character representing a digit in the pattern. NumberFormatter::MINUS_SIGN_SYMBOL The minus sign. NumberFormatter::PLUS_SIGN_SYMBOL The plus sign. NumberFormatter::CURRENCY_SYMBOL The currency symbol. NumberFormatter::INTL_CURRENCY_SYMBOL The international currency symbol. NumberFormatter::MONETARY_SEPARATOR_SYMBOL The monetary separator. NumberFormatter::EXPONENTIAL_SYMBOL The exponential symbol. NumberFormatter::PERMILL_SYMBOL Per mill symbol. NumberFormatter::PAD_ESCAPE_SYMBOL Escape padding character. NumberFormatter::INFINITY_SYMBOL Infinity symbol. NumberFormatter::NAN_SYMBOL Not-a-number symbol. NumberFormatter::SIGNIFICANT_DIGIT_SYMBOL Significant digit symbol. NumberFormatter::MONETARY_GROUPING_SEPARATOR_SYMBOL The monetary grouping separator.

Rounding mode values used by numfmt_get_attribute() and numfmt_set_attribute() with NumberFormatter::ROUNDING_MODE attribute. NumberFormatter::ROUND_CEILING Rounding mode to round towards positive infinity. NumberFormatter::ROUND_DOWN Rounding mode to round towards zero. NumberFormatter::ROUND_FLOOR Rounding mode to round towards negative infinity. NumberFormatter::ROUND_HALFDOWN Rounding mode to round towards «nearest neighbor» unless both neighbors are equidistant, in which case round down. NumberFormatter::ROUND_HALFEVEN Rounding mode to round towards the «nearest neighbor» unless both neighbors are equidistant, in which case, round towards the even neighbor. NumberFormatter::ROUND_HALFUP Rounding mode to round towards «nearest neighbor» unless both neighbors are equidistant, in which case round up. NumberFormatter::ROUND_UP Rounding mode to round away from zero.

Читайте также:  Css make background image darker

Pad position values used by numfmt_get_attribute() and numfmt_set_attribute() with NumberFormatter::PADDING_POSITION attribute. NumberFormatter::PAD_AFTER_PREFIX Pad characters inserted after the prefix. NumberFormatter::PAD_AFTER_SUFFIX Pad characters inserted after the suffix. NumberFormatter::PAD_BEFORE_PREFIX Pad characters inserted before the prefix. NumberFormatter::PAD_BEFORE_SUFFIX Pad characters inserted before the suffix.

See Also

Table of Contents

  • NumberFormatter::create — Create a number formatter
  • NumberFormatter::formatCurrency — Format a currency value
  • NumberFormatter::format — Format a number
  • NumberFormatter::getAttribute — Get an attribute
  • NumberFormatter::getErrorCode — Get formatter’s last error code
  • NumberFormatter::getErrorMessage — Get formatter’s last error message
  • NumberFormatter::getLocale — Get formatter locale
  • NumberFormatter::getPattern — Get formatter pattern
  • NumberFormatter::getSymbol — Get a symbol value
  • NumberFormatter::getTextAttribute — Get a text attribute
  • NumberFormatter::parseCurrency — Parse a currency number
  • NumberFormatter::parse — Parse a number
  • NumberFormatter::setAttribute — Set an attribute
  • NumberFormatter::setPattern — Set formatter pattern
  • NumberFormatter::setSymbol — Set a symbol value
  • NumberFormatter::setTextAttribute — Set a text attribute

Источник

PHP — Number Formatting, No Rounding

I was replying to someone elses question about number formatting and came up with a class that will format a number of any length with decimal or not, without rounding. It works perfectly, but after a comment by a noteworthy member my curiosity was aroused.

Is there an easier/faster/better way to achieve this??

/*GHETTO NUMBER FORMATTER v1.0*/

private $input;
private $output;

public $whole;
public $decimal;
public $number;

function __construct($input) $this->input = $input;
$this->output = $output;
$this->number = $number;
$this->whole = $whole;
$this->decimal = $decimal;
>

private function ghettoFormat()

//Split number at the .(decimal)
$this->number = explode(‘.’, $this->input);
//Define the whole number
$this->whole = $this->number[0];
//Format the whole number
$this->whole = number_format($this->whole, 0,», ‘,’);
//Define the decimal
$this->decimal = $this->number[1];
//Format the decimal
$this->decimal = rtrim($this->decimal, ‘0’);

if(is_string($this->input)) $this->output = $this->decimal != » ? $this->whole.’.’.$this->decimal : $this->whole;
>
else $this->output = ‘ERROR: Input arg must be passed as type: string‘;
>

//Return result
return ‘Original — ‘.$this->input.’

GhettoFormat — ‘.$this->output.’

‘;

public function makeItGhetto() return $this->ghettoFormat();
>
>

$ghettoFormat = new ghettoNumber(‘12565456565.123401201001210000’);
print $ghettoFormat->makeItGhetto();

I will reiterate my question once more.
Is there, in your opinion, a BETTER way to achieve this (number formatting w/o rounding)?

Similar Tutorials

Number Format And Rounding

Can anyone show me why my script is still rounding up / down?

Читайте также:  Custom post types php

Do I have a syntax error as I cannot get my math equation to display more than 2 decimal places even though it’s set to 3?

Any help or advice would be really great.

Number Formatting

Hi there,
I need a PHP number format to match one and all of these sequence of numbers:
8500.00
9999.99
15.00
0.00

Number Formatting

Hi, I want to chenge the following statement so that numbers ending
with «.00» are changed to blank. Many opinions but I haven’t got any
to work. Any help?

Invalid Parameter Number: Number Of Bound Variables Does Not Match Number Of Tokens

I’m getting the dreaded » Invalid parameter number: number of bound variables does not match number of tokens» error and I’ve looked at this for days. Here is what my table looks like:

| id | int(4) | NO | PRI | NULL | auto_increment | | user_id | int(4) | NO | | NULL | | | recipient | varchar(30) | NO | | NULL | | | subject | varchar(25) | YES | | NULL | | | cc_email | varchar(30) | YES | | NULL | | | reply | varchar(20) | YES | | NULL | | | location | varchar(50) | YES | | NULL | | | stationery | varchar(40) | YES | | NULL | | | ink_color | varchar(12) | YES | | NULL | | | fontchosen | varchar(30) | YES | | NULL | | | message | varchar(500) | NO | | NULL | | | attachment | varchar(40) | YES | | NULL | | | messageDate | datetime | YES | | NULL |
$params = array( ':user_id' => $userid, ':recipient' => $this->message_vars['recipient'], ':subject' => $this->message_vars['subject'], ':cc_email' => $this->message_vars['cc_email'], ':reply' => $this->message_vars['reply'], ':location' => $this->message_vars['location'], ':stationery' => $this->message_vars['stationery'], ':ink_color' => $this->message_vars['ink_color'], ':fontchosen' => $this->message_vars['fontchosen'], ':message' => $messageInput, ':attachment' => $this->message_vars['attachment'], ':messageDate' => $date );
$sql = "INSERT INTO messages (user_id,recipient, subject, cc_email, reply, location,stationery, ink_color, fontchosen, message,attachment) VALUES( $userid, :recipient, :subject, :cc_email, :reply, :location, :stationery, :ink_color, :fontchosen, $messageInput, :attachment, $date);";
And lastly, here is how I am calling it: $dbh = parent::$dbh; $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); if (empty($dbh)) return false; $stmt = $dbh->prepare($sql); $stmt->execute($params) or die(print_r($stmt->errorInfo(), true)); if (!$stmt) < print_r($dbh->errorInfo()); >

I know my userid is valid and and the date is set above (I’ve echo’d these out to make sure). Since the id is auto_increment, I do not put that in my sql (though I’ve tried that too), nor in my params (tried that too). What am I missing? I feel certain it is something small, but I have spent days checking commas, semi-colons and spelling. Can anyone see what I’m doing wrong?

Источник

Оцените статью