i want to display a Quadratic equation with format like (4x2). i want to display 2 as superscript automatically after typing x in the textbox.
+3
A:
Unicode offers superscript digits.
\u2070
= ⁰\u00B9
= ¹\u00B2
= ²\u00B3
= ³\u2074
= ⁴\u2075
= ⁵\u2076
= ⁶\u2077
= ⁷\u2078
= ⁸\u2079
= ⁹
So simply replace a 2 that's supposed to be superscript with U+00B2.
4x² - 2x + 4
Here's some sample code:
private static const SUPER_DIGIT:Object = {
'0':0x2070, '1':0x00B9, '2':0x00B2, '3':0x00B3, '4':0x2074,
'5':0x2075, '6':0x2076, '7':0x2077, '8':0x2078, '9':0x2079,
'-':0x207B
};
// Converts all 0123456789- characters to their superscript forms.
public static function toSuperscript(str:String):String {
var buffer:Array = new Array(str.length);
for (var i:int = 0; i < str.length; ++i) {
buffer[i] = SUPER_DIGIT[str.charAt(i)];
if (buffer[i] == null) buffer[i] = str.charCodeAt(i);
}
return String.fromCharCode.apply(null, buffer);
}
Edit:
If you passed my method 4x2
it'd return ⁴x²
. What you need to do is--within an update event on your text field--detect substrings that need to be converted to superscript and replace them. A quick rule that'll work for you is to say "if a digit comes immediately after a character with a code equal to or greater than 'A' (\u0041), replace it with its superscript equivillant."
Gunslinger47
2010-07-18 02:25:24
i don't know how to link your code with dynamic text field. could you please mention about that?
Penelope
2010-07-20 16:43:11
@Pen: I added an explanation to the end of my answer.
Gunslinger47
2010-07-20 22:15:16