最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to add the degrees celcius symbol in JavaScript - Stack Overflow

programmeradmin4浏览0评论

I am trying to include the degrees Celsius symbol (°C) in my code, this is what I have and it is not working:

$('#temp').unbind().append((Math.round(temperature - 273.15)) + '&#8451');

I am trying to include the degrees Celsius symbol (°C) in my code, this is what I have and it is not working:

$('#temp').unbind().append((Math.round(temperature - 273.15)) + '&#8451');

Share Improve this question asked Sep 12, 2015 at 20:58 Jon DanJon Dan 951 gold badge1 silver badge10 bronze badges 4
  • try with this °C – Kristijan Iliev Commented Sep 12, 2015 at 21:00
  • Alternatively, you're missing a semicolon after your &#8451 try ℃ (tested and working) – CollinD Commented Sep 12, 2015 at 21:01
  • 4 Why not just '°C'? – Bergi Commented Sep 12, 2015 at 21:14
  • @Bergi Thank You. I did not have that circle symbol so I was looking for the asci code as well. But this saves my day. – Pratik Thorat Commented Dec 29, 2023 at 22:17
Add a comment  | 

8 Answers 8

Reset to default 8

You just miss ; in ℃

An HTML entity has the format &name; as pointed out already by Arsen. Here is an alternative found here:

var temperature = 305;
$('#temp').unbind().append((Math.round(temperature - 273.15)) + '°');
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<div id="temp">Temperature in degrees Celsius is </div>

I simply copied the degrees symbol directly from the Character Editor in Windows.

Start --> symbols --> character map

Then select copy and paste it into your code ...

ctx.fillText(degrees.toFixed(2) +"º", x, y + 50);

Another approach is to utilize the in-built Int.NumberFormat Javascript object:

const number = 100;

const degrees = new Intl.NumberFormat('en-US', {
  style: 'unit',
  unit: 'celsius',
});

console.log(degrees.format(number));

You can review the documentation here

use sup tag to place O it above a text.

e.g.

<p>31<sup>O</sup>C</p>

I realise that this question was asked six years and ten months ago but if anyone stumbles upon this looking for what I was, I found the following to be helpful.

Use the JavaScript method codePointAt() to retrieve the code point integer from the symbol:

let unicodeDegCel = '℃'.codePointAt(0);

This will return the integer 8451. Now use the JavaScript method fromCodePoint() prepended with String, using the character code retrieved from codePointAt():

let degreesCelcius = String.fromCodePoint(8451);

This is was what eventually enabled me to use the degrees celcius symbol in ChartJS.

Source: Convert a character to its ASCII code in JavaScript

The hexadecimal code "\xB0" generates that Celsius/Fahrenheit sign. :)

var degfahren = prompt("suggest a number" , 50);
var degcelc = (degfahren - 32) * 5\9;
console.log(degfahren+"\xB0 deg Fahrenheit "+"is eqaul to the "+degcelc+"\xB0 celcius");

To get the celsius symbol, this will work as of 2024: "\u2103"

发布评论

评论列表(0)

  1. 暂无评论