You could style it with overflow: hidden, but then users will not see the entire result.
To accurately display the information you should reduce font, until a minimum threshold, so that the result remains in view.
If the number still does not fit, maybe resort to scientific notation.
If you are not using a monospaced calculating an elements render dimensions is a difficult problem, and is usually done using some kind of dummy hidden element.
But since you have full control over the styles applied, and you are using a monospaced font, you can just do some math to calculate the required font size for it to fit.
Because the font is monospaced you know each character is the same width. Therefore you can see what the aspect ratio of a character is (width / height), because font-size does not refer to the width of the character.
Then taking into account the amount of padding you set, you can determine the number of digits that can fit.
Calculator viewport width > padding + character width * number of characters
However, when you calculate big numbers, you will eventually need to do scientific notation, if you don't want to have a very small font, and 1 number on multiple rows.
Also, humans don't really comprehend really large numbers visually, and exponents help.