# convert-length

> Converts a distance unit (e.g. m) to another (e.g. cm)

Latest version **1.0.1** (published 2018-07-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install convert-length
pnpm add convert-length
yarn add convert-length
bun add convert-length
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2018-07-29 |
| First published | 2018-07-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 6.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 25 |
| Author | Matt DesLauriers |
| Maintainers | mattdesl |
| Keywords | distance, length, px, conversion, convert, converts, from, to, unit, units, m, cm, mm, meter, meters, inches, in, centimeter, millimetre, centimetre, metre, metres, inch, millimeter |

## Links

- npm: https://www.npmjs.com/package/convert-length
- Repository: https://github.com/mattdesl/convert-length
- Issues: https://github.com/mattdesl/convert-length/issues
- npm.io page: https://npm.io/package/convert-length

## Dependencies (1)

- [defined](https://npm.io/package/defined.md) ^1.0.0

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 1.0.1 (latest) — 2018-07-29
- 1.0.0 — 2018-07-29

## README

# convert-length

A simple utility to convert from physical lengths (meters, inches, etc) to pixels and back, based on the CSS spec. Supports converting to and from the following units:

- `mm` (millimeters)
- `cm` (centimeters)
- `m` (meters)
- `pc` (pica, or 1 / 6 of an inch)
- `pt` (point, or 1 / 72 of an inch)
- `in` (inch)
- `ft` (feet, or 12 inches)
- `px` (pixels)

Example:

```js
const convert = require('convert-length');

// Convert 10 inches to meters
const result = convert(10, 'in', 'm')
// -> 0.254

// Convert A4 print (210 x 297 mm) to pixels @ 300 PPI
const result = [ 210, 297 ].map(n => {
  return convert(n, 'mm', 'px', { pixelsPerInch: 300 })
});
// -> [ 2480, 3508 ]
```

Pixels are computed based on the specified `pixelsPerInch` setting (default 96), as per the CSS spec.

## Install

Use [npm](https://npmjs.com/) to install and use this. Should work with browserify, Webpack, etc.

```sh
npm install convert-length
```

## Usage

### `result = convert(value, fromUnit, toUnit, [options])`

Converts the `value` number from the `fromUnit` unit string (e.g. `"in"`) to the `toUnit` unit string. The unit strings are case insensitive.

Options can be:

- `pixelsPerInch` (default 96) the number of pixels in one inch, used when converting to and from `"px"` units
- `precision` if specified, the value will be rounded to the Nth decimal. e.g. A precision of `3` will round to `0.001`. If not specified, the result will not be rounded.
- `roundPixel` (default true) If enabled, when converting to a `"px"` unit the return value will be rounded to a whole pixel. If disabled, the conversion will instead round to the specified Nth `precision` decimal (or no rounding if `precision` is not specified).

### `convert.units`

The list of supported units for this module, equivalent to:

```js
[ 'mm', 'cm', 'm', 'pc', 'pt', 'in', 'ft', 'px' ]
```

## See Also

This module was inspired by [measures](https://www.npmjs.com/package/measures) and [convert-units](https://www.npmjs.com/package/convert-units), but I wanted something dead-simple for the browser, without all the extra features, and that supports pixels in the same way CSS and Photoshop handle their conversions.

## License

MIT, see [LICENSE.md](http://github.com/mattdesl/convert-length/blob/master/LICENSE.md) for details.

---
_Source: https://npm.io/package/convert-length · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
