Using the Units Object for formatting


<< Significant issues (Creat ...
Back to Portfolio FAQ index
Why do dragged images get ... >>

Manual:NP manual pages 41-42

NP offers 4 methods in its API for formatting data info various styles, via the API's Units object. There are methods for Dates, Length, Currency and Size. The Length and Size method deserve a little extra explanation.

convertLength

Syntax: Units.convertLength(value,to,dpi)

The primary purpose of the Length method - though not apparent from the title - is to convert pixel values to values usingother measurement types.

The convertLength function converts the specified pixel length (value) using one of following format strings (to) : inches, cm, mm, points, and picas. The returned value of the function is a string. Dots-per-inch (dpi) is required - although if not supplied NP assumes a default value of 72.0. This is because the underlying JavaScript functions used for unit formatting is open source code incorporated into global.np and it is the latter code that requires the dpi value. To allow for the fact a record might not have any resolution information, NP supplies a value of 72.0 if a dpi parameter is not set by the user's code.

Examples:

Get the width of a record in inches

<%= Units.convertLength(RecordSet.record.get('Width'), 'inches', RecordSet.record.get('Horizontal Resolution')) %>

// for example this gives '2560' (px) on screen
<%= RecordSet.record.get('Width') %>
// this gives '203.11' (mm) on screen
<%= Units.convertLength(RecordSet.record.get('Width'),'mm',RecordSet.record.get('Horizontal Resolution')) %>
// this gives '203' (mm) on screen
<%= parseInt(Units.convertLength(RecordSet.record.get('Width'),'mm',RecordSet.record.get('Horizontal Resolution')) ,10)%>
// a more finished example 'Width: 203 mm' with an enclosing <p> tag.
<p>Width: <%= parseInt(Units.convertLength(RecordSet.record.get('Width'),'mm',RecordSet.record.get('Horizontal Resolution')) ,10)%> mm</p>

convertSize

Syntax: Units.convertSize(value,to,[comma],[decimal])

While the convertSize method appears to be primarily for converting file size info, it can also be used to convert formatting of numbers in general. By converting an 'ordinary' number - such as RecordSet.totalItems in a using the bytes 'to' format, you are effectively doing a number to formatted-number-string format conversion allowing you to add/remove commas or decimal places as the second example below shows. This saves writing a custom JS routine to format ordinary (non byte count) numbers.

This function converts the specified number value (value) in bytes [sic] to the specified size (to) using one of the mandatory following format strings: bytes, kbytes, and mbytes. The returned value is a string. If comma (boolean) is true, the number is formatted as a string with commas in the appropriate places (default = true). decimal (number) specifies how many digits are displayed to the right of the decimal point (default is 0 unless the to value is mbytes in which case it is 2).

Examples:

Get the size of a file in megabytes, to 2 decimal places.
Note that value here is kb so it is first multiplied by 1024 to turn it back to bytes.

<%= Units.convertSize(RecordSet.record.get('File Size') * 1024, 'mbytes', true, 2) %>

Convert number 1234567.89 into string '1,234,567' with commas and no decimal places
<%= Units.convertSize(1234567.89, 'bytes', true, 0) %>
You might as easily use a variable holding a number, e.g. convert var myNum
var myNum = RecordSet.totalItems; // = 1234567.89 in this example
<%= Units.convertSize(myNum, 'bytes', true, 0) %>
Result is the string '1,234,567'

Keywords (to assist indexing/searching):



Question: Using the Units Object for formatting [FAQ00386.htm]
Last Update:- 27 November 2006


<< Significant issues (Creat ...
Back to Portfolio FAQ index
Why do dragged images get ... >>

Quick Search of PortfolioFAQ (word, words or phase): or try the Advanced Search

User-to-User Forums  |  Report error/typo/broken link  |  Request new topic  |  Ask a question

Site and articles © Mark Anderson 2001-2007 - Visit my home page


This FAQ is created and maintained using
Friday logo
Friday - The Automatic FAQ Maker