fwrawx
fwrawx

Reputation: 181

How to represent negative number in HTML

Simple question, what is the proper character to use for representing a negative number? Should I use a normal dash, a minus entity or is there a more appropriate entity to use?

To be more clear, in an HTML document if I want to display a negative temperature, should I use:

-5 °C

or

−5 °C

or something else?

Upvotes: 5

Views: 7406

Answers (3)

spekary
spekary

Reputation: 438

The other answers give you the correct information on the html, in that there are character codes that mean "minus", and the downside of using the hyphen is that the browser could word-wrap the number and put the digits on the line after the hyphen.

However, you asked "Should I use a normal dash"? That really depends on the context. In particular, if you want the user to be able to copy your text and paste it into another program, and have that program interpret your negative numbers as negative numbers, you are going to have to use a hyphen.

For example, copy the following two lines and paste it into an Excel spreadsheet:

−45

-45

You will notice that the first number is treated as text, and the second is treated as a number, even though according to the html, the opposite should happen.

To use a hyphen with negative numbers, and prevent wrapping, use a white-space style of "nowrap".

Upvotes: 1

Jukka K. Korpela
Jukka K. Korpela

Reputation: 201708

There are two separate questions here: 1) what do you use as the character that acts as the sign of a negative number (or, more generally, how you write a negative number with characters), and 2) how do you represent that character in HTML. Only the latter is HTML-specific and can thus be considered on-topic at SO.

However, question 1 is, too, somewhat programming-related. In most computer languages (such as JavaScript, HTML, and CSS), a negative number is almost always written using the common ASCII hyphen, officially HYPHEN-MINUS, “-”, U+002D. For this reason, people often use the ASCII hyphen in general texts, too, and even in mathematical texts. This typically violates the rules of human languages. In most languages, the MINUS SIGN “−” U+2212 is preferred. It is also typographically much better, especial in quality fonts, where e.g. “−42” has a noticeable sign and in “-42” the ASCII hyphen is less noticeable. The MINUS SIGN also has better line breaking properties: web browsers do not treat it as allowing a line break between it and the following digit, as they may well do to the ASCII hyphen.

Having chosen to use the minus sign, the simplest approach is to use the character “−” itself. For this, you need some method of inputting it. You also need to take care of character encoding issues, normally using UTF-8, but this is something that should be done anyway.

You can also use the named character reference −. It stands for the minus sign, and it might be convenient casually when you need to use the character but lack a convenient quick way of typing it.

Upvotes: 6

Zaheer Ahmed
Zaheer Ahmed

Reputation: 28568

Here you can find different operators representation in html.

some

Upvotes: 2

Related Questions