Documentation

Outputting measurements - Zend_Measure

Outputting measurements

Measurements can be output in a number of different ways.

Automatic output

Outputting values

Output with unit of measurement

Output as localized string

Automatic output

Zend_Measure supports outputting of strings automatically.

Example #1 Automatic output

  1. $locale = new Zend_Locale('de');
  2. $mystring = "1.234.567,89";
  3. $unit = new Zend_Measure_Length($mystring,
  4.                                 Zend_Measure_Length::STANDARD,
  5.                                 $locale);
  6.  
  7. echo $unit;

Note: Measurement output
Output can be achieved simply by using » echo or » print.

Outputting values

The value of a measurement can be output using getValue().

Example #2 Output a value

  1. $locale = new Zend_Locale('de');
  2. $mystring = "1.234.567,89";
  3. $unit = new Zend_Measure_Length($mystring,
  4.                                 Zend_Measure_Length::STANDARD,
  5.                                 $locale);
  6.  
  7. echo $unit->getValue();

The getValue() method accepts an optional parameter round which allows to define a precision for the generated output. The standard precision is '2'.

Output with unit of measurement

The function getType() returns the current unit of measurement.

Example #3 Outputting units

  1. $locale = new Zend_Locale('de');
  2. $mystring = "1.234.567,89";
  3. $unit = new Zend_Measure_Weight($mystring,
  4.                                 Zend_Measure_Weight::POUND,
  5.                                 $locale);
  6.  
  7. echo $unit->getType();

Output as localized string

Outputting a string in a format common in the users' country is usually desirable. For example, the measurement "1234567.8" would become "1.234.567,8" for Germany. This functionality will be supported in a future release.

Copyright

© 2006-2021 by Zend by Perforce. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts