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

html - how can we make a digit after decimal appear like a superscript in javascript? - Stack Overflow

programmeradmin0浏览0评论

how can we make a digit after decimal appear like a superscript in java.

here is the example

I have the price say suppose $14.98, is there a way that i can print those .98 as supercript.

thank you

this is how i am getting price value <%=price.getBasePrice().setScale(2, BigDecimal.ROUND_HALF_UP).toString()%>

how can we make a digit after decimal appear like a superscript in java.

here is the example

I have the price say suppose $14.98, is there a way that i can print those .98 as supercript.

thank you

this is how i am getting price value <%=price.getBasePrice().setScale(2, BigDecimal.ROUND_HALF_UP).toString()%>

Share Improve this question edited Oct 4, 2011 at 17:11 nyshangal asked Oct 4, 2011 at 16:51 nyshangalnyshangal 1022 silver badges8 bronze badges 6
  • 2 Where/how are you outputting that value? In the console? In a Swing application? In HTML from a JSP? – Freiheit Commented Oct 4, 2011 at 16:52
  • 4 java or javascript? big difference.... – Jason S Commented Oct 4, 2011 at 16:52
  • 2 FYI, JavaScript is not a portmanteau of Java and superscript. – BoltClock Commented Oct 4, 2011 at 16:53
  • Like a price, or as a superscript? Superscripts are smaller, but easier. – Ed Staub Commented Oct 4, 2011 at 16:58
  • OK, that looks like Java + server-side script rather than Javascript. @user968951: in the future, please don't edit the OP's tags w/o getting the OP to clarify. – Jason S Commented Oct 4, 2011 at 17:25
 |  Show 1 more ment

4 Answers 4

Reset to default 3

You can superscript in Javascript by using the .sup() function:

<script>
var j = "14.98";
j = j.split(".");

document.write("$" + j[0] + j[1].sup());
</script>

Which Prints: $1498

$('.price').each(function () {
    var $this = $(this),
        $val = $this.text(),
        dec_pos = $val.indexOf('.');
    $this.html($val.substring(0, dec_pos) + '<sup>' + $val.substring(dec_pos + 1) + '</sup>');
});

http://jsfiddle/tD9PW/

If you want to output to an HTML page (and I assume you do if you mention Javascript), there is a tag <sup> that would do the job:

<span>14.<sup>98</sup></span>

It's pure HTML, not Javascript, but you can use Javascript to generate this HTML.

You do NOT need to use the decimal point when the cents are superscripted. This is particularly true when the type size is very large on posters or billboards.

发布评论

评论列表(0)

  1. 暂无评论