开发者

How to display 2^3 in Javascript

开发者 https://www.devze.com 2023-03-22 04:00 出处:网络
I am building an app using appcelerator which needs to display a mathematical expression e.g 2^3 but with the 3 being a small number above the 2. Is there a way to express mathematical expressions in

I am building an app using appcelerator which needs to display a mathematical expression e.g 2^3 but with the 3 being a small number above the 2. Is there a way to express mathematical expressions in Javascript?

Notes

  • Th开发者_JAVA百科e text is a static bit of text.
  • Language in use is Javascript.
  • Runs on the iPhone/Android simulator in the appcelerator framework.
  • Appcelerator escapes HTML

Thanks in advance for any assistance.


"2"+"3".sup() should give you the correct formatting.

You can also use .sub() for subscript.


if you are concerned only about the display, you can make use of the html tags <sub></sub> and <sup></sup> which makes the text between them subscript and superscript.
A big plus is that the native javascript string has the functionality of returning a string wrapped into theese tags (not that it would've been very hard to implement):

var a = "2",
    b = "3",
    formattedEquation = a+b.sup(),
    r = Math.pow(a,b),
    htmlEquation = formattedEquation + ' = ' + r;
// let's say you use jquery for the simplicity of the example
$(myContainer).html(htmlEquation);


For more elaborate mathematical typesetting, you might be able to use the MathJax JavaScript display engine. It looks to be fully compatible with WebKit and seems to typeset things fine on my iOS devices.


There are the <sup> and the <sub> tags in HTML, try this:

<html>x<sup>4</sup>+y<sup>1245687</sup></html>

If this isn't enough, take a look at MathML


Some good ideas which would work on platforms like JQueryMobile but unfortunately Appcelerator escapes all html (Unless your coding just for the Android).

The way to display the mathematical expressions is by defining the unicode character in the string as suggested by Pat in a comment.

E.g.

2\u00B3
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号