files/en-us/web/api/svglength/converttospecifiedunits/index.md
{{APIRef("SVG")}}
The convertToSpecifiedUnits() method of the {{domxref("SVGLength")}} interface allows you to convert the length's value to the specified unit type.
This function will:
convertToSpecifiedUnits(unitType)
unitType
SVG_LENGTHTYPE_UNKNOWN.
SVGLength.SVG_LENGTHTYPE_NUMBER: convert to unitless numberSVGLength.SVG_LENGTHTYPE_PERCENTAGE: convert to percentageSVGLength.SVG_LENGTHTYPE_EMS: convert to em unitsSVGLength.SVG_LENGTHTYPE_EXS: convert to ex unitsSVGLength.SVG_LENGTHTYPE_PX: convert to pixelsSVGLength.SVG_LENGTHTYPE_CM: convert to centimetersSVGLength.SVG_LENGTHTYPE_MM: convert to millimetersSVGLength.SVG_LENGTHTYPE_IN: convert to inchesSVGLength.SVG_LENGTHTYPE_PT: convert to pointsSVGLength.SVG_LENGTHTYPE_PC: convert to picasNone ({{jsxref('undefined')}}).
// Get an SVGLength object
const svg = document.querySelector("svg");
const length = svg.createSVGLength();
// Set a length value in centimeters
length.valueAsString = "0.5cm";
// Convert the length to millimeters
length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_MM);
console.log(length.unitType); // Output: 7 (SVG_LENGTHTYPE_MM)
console.log(length.valueInSpecifiedUnits); // Output: 5
console.log(length.valueAsString); // Output: "5mm"
{{Specifications}}
{{Compat}}