|
- Author:
- kris (IP: 68.122.193.31)
- Timestamp:
- 04/17/07 02:50:47 (17 years ago)
- Comment:
updated
Legend:
- Unmodified
- Added
- Removed
- Modified
-
TutTextConversions
v11 |
v12 |
|
7 | 7 | == Number Conversions == |
---|
8 | 8 | |
---|
| 9 | lightweight numeric conversions are located in modules tango.text.convert.Integer and tango.text.convert.Float: |
---|
9 | 10 | {{{ |
---|
10 | 11 | #!d |
---|
11 | | |
---|
12 | 12 | // from text to numbers |
---|
13 | | |
---|
14 | | int integer = Atoi.parse("302"); |
---|
15 | | double double_ = Double.parse("3.14592"); |
---|
16 | | real real_ = DGDouble.parse("1.41421"); // for extended accuracy |
---|
| 13 | int i = Integer.parse("302"); |
---|
| 14 | real d = Float.parse("3.14159"); |
---|
17 | 15 | |
---|
18 | 16 | // and from numbers to text |
---|
19 | | |
---|
20 | | char [64] intBuffer; |
---|
21 | | char [64] floatBuffer; |
---|
22 | | char [64] realBuffer; |
---|
23 | | |
---|
24 | | char [] intString = Integer.format(intBuffer,32 ); |
---|
25 | | char [] floatString = Double.format(floatBuffer,45 ); |
---|
26 | | char [] realString = DGDouble.format(realBuffer,32.65798,3 ); |
---|
27 | | |
---|
| 17 | char[] intString = Integer.toUtf8(32); |
---|
| 18 | char[] floatString = Float.toUtf8(3.14159); |
---|
28 | 19 | }}} |
---|
29 | 20 | |
---|
30 | 21 | == Unicode == |
---|
31 | 22 | We will continue with the other most common types, which would be converting between different unicode representations. Sticking to tango tradition of using 'toUtf8' to mean 'give me a string representation' , tango.text.conversion.Unicode contains templated methods to convert between different encodings including: |
---|
32 | | |
---|
33 | 23 | {{{ |
---|
34 | 24 | #!d |
---|
35 | | |
---|
36 | | char [] eight = Unicode.toUtf8("test"); |
---|
37 | | wchar [] sixteen = Unicode.toUtf16("test"); |
---|
38 | | dchar [] thirtyTwo = Unicode.toUtf32("test"); |
---|
39 | | |
---|
| 25 | char[] eight = Unicode.toUtf8("test"); |
---|
| 26 | wchar[] sixteen = Unicode.toUtf16("test"); |
---|
| 27 | dchar[] thirtyTwo = Unicode.toUtf32("test"); |
---|
40 | 28 | }}} |
---|
41 | 29 | |
---|
42 | | == Formating == |
---|
| 30 | == Formatting == |
---|
43 | 31 | |
---|
44 | | Probably the most useful class in the conversion line-up is the 'Formatter' class, which formats a string based on C# style syntax via the 'format' method. |
---|
45 | | |
---|
| 32 | Probably the most useful class in the conversion line-up is the 'Layout' class, which formats a string based on C# style syntax. |
---|
46 | 33 | {{{ |
---|
47 | 34 | #!d |
---|
48 | | |
---|
49 | | char [] str = Formatter.format("{0} sells {1} by the {2}","Sally","shells","seashore"); |
---|
50 | | |
---|
| 35 | auto layout = new Layout!(char); |
---|
| 36 | char[] str = layout ("{} sells {} by the {}","Sally", "shells", "seashore"); |
---|
51 | 37 | }}} |
---|
52 | 38 | |
---|
55 | 41 | == Summary == |
---|
56 | 42 | |
---|
57 | | The tango.text.convert package contains a set of quality classes for both precison converting ( DGDouble ) , and every day converting ( Atoi, Double, Integer ). The convention of using parse() and format() among all the classes makes it easy to just dive right in with a conversion class. |
---|
| 43 | The tango.text.convert package contains a set of classes for everyday numeric convertion (Float, Integer). The convention of using parse() and format() among all the classes makes it easy to just dive right in with a conversion class. |
---|
58 | 44 | |
---|
59 | | Other more exotic classes exist for converting dates, and for inserting Unicode BOM's which you can find in the [wiki:ChapterConversions Reference Manual - Conversions chapter]. |
---|
| 45 | Other more exotic classes exist for converting dates, handling Unicode BOM, etc. They can be located via the [wiki:ChapterConversions Reference Manual - Conversions chapter]. |
---|
60 | 46 | |
---|
61 | 47 | |
|
|
|
|
|
Copyright © 2006-2024 Tango. All Rights Reserved. | Page Width:
Static or
Dynamic