I am trying to find the best way to get my screen size in inches. I'm testing it on a Galaxy S10, which has a screen size of 6.1". However, if you test this code below (which is also in this codepen), I get a result of 3.75" x 7.92".
Using the floor()
method rounds it down, but not to the 6.1" that I should get.
Any ideas?
//get screen inches
function getScreenSizeInches() {
const dpi_x = dpi.offsetWidth;
const dpi_y = dpi.offsetHeight;
const inchesWidth = (screen.width / dpi_x).toFixed(2);
const inchesHeight = (screen.height / dpi_y).toFixed(2);
inchesSize.textContent = `${inchesWidth}" x ${inchesHeight}"`;
}
getScreenSizeInches();
#dpi {
height: 1in;
width: 1in;
left: 100%;
position: fixed;
top: 100%;
}
<div id="dpi"></div>
<h2>Screen Size: <span id="inchesSize"></span></h2>