You are on page 1of 26

Numbers

Java SE
Contents

• Number Classes

• Formatting Numbers

• Beyond Fundamental Calculation


Number Classes
Primitive Wrapper Argument Types of Constructor

byte Byte byte or String

short Short short or String

int Integer int or String

long Long long or String

float Float float or String

double Double double or String


Methods

Getting Converting to
Comparing Check Equality
Primitives other types
Getting Primitives
Return Type Method
byte byteVaue()
short shortValue()
int intValue()
long longValue()
float floatValue()
double doubleValue()
Comparing
Return Type Method
int compare(Byte anotherByte)
int compare(Short anotherShort)
int compare(Integer anotherInteger)
int compare(Long anotherLong)
int compare(Float anotherFloat)
int compare(Double anotherDouble)
Check Equality
Return Type Method
boolean equals(Object obj)
Convert to other types
Method Description
String မ"eန Inetger အ&ဖစ)e&ပ+င)-လည)- eပ-0iuင)ပ3သည)။ 

Decimal, Octal 0"င)6 Hexdecimal ပu7စ7&ဖင)6 eဖ+)&ပထ+-eသ+
static Integer decode(String s)
String မ9+-အ+- Integer အ&ဖစ)e&ပ+င)-လည)- eပ-0iuင)
ပ3သည)။

Decimal ပu7စ7&ဖင)6 eရ-သ+-eဖ+)&ပထ+-eသ+ String အ+- int


static int parseInt(String s)
အ&ဖစ)e&ပ+င)-eပ-0iuင)ပ3သည)။

Radix တ<င) eဖ+)&ပထ+-eသ+ ကiန)-ဂဏန)- (10, 2, 8, 16)


static int parseInt(String s, int
အလiuက) eဖ+)&ပထ+-eသ+ String အ+- int အ&ဖစ)e&ပ+င)-eပ-
radix)
0iuင)ပ3သည)။

Integer ၏ တန)ဖiu-အ+- String Object အeနန"င)6 e&ပ+င)-eပ-


String toString()
0iuင)ပ3သည)။
Convert to other types
Method Description
Int I ၏ တန)ဖiu-အ+- String Object အeနန"င)6 e&ပ+င)-eပ-0iuင)
static String toString(int i)
ပ3သည)။

Primitive Type i ၏ တန)ဖiu-အ+- Integer အ&ဖစ) e&ပ+င)-


static Integer valueOf(int i)
လည)- eပ-ပ3သည)။

Integer အ&ဖစ)eဖ+)&ပထ+-eသ+ String ၏ တန)ဖiu-အ+-


Static Integer valueOf(String s)
Integer အ&ဖစ) e&ပ+င)-လည)-eပ-0iuင)ပ3သည)။

Radix တ<င) သတ)မ"တ)ထ+-eသ+ ကiန)-ဂဏန)-eဖ+)&ပပu7


static Integer valueOf(String s,
အတiuင)- eရ-သ+-သ+-eသ+ String အ+- Integer အ&ဖစ)
int radix)
e&ပ+င)-လည)- eပ-0iuင)ပ3သည)။
Formatting Numbers

printf & format DecimalFormat


methods class
printf & format
System.out
<<System Output Stream>> PrintStream
printf format
format

• Java ဘ+သ+ရပ)တ<င) Number မ9+-အ+- Format ခ9ရန)အတ<က) PrintStream#format,


System.out ၏ printf 0"င)6 format method မ9+-အ+- အသu7-&ပuDကသည)။

• System.out ၏ printf 0"င)6 format တiu၏ ဖ<F6စည)-ပu7eက+၊ လuပ)eဆ+င)မI6ရလဒ)သည) အတKတK


ပင) &ဖစ)၏။

• System.out ၏ အတ<င)-တ<င) PrintStream အ+- eခLယK အသu7-&ပueနeသ+eDက+င)6


လuပ)eဆ+င)ပu7မ"+ အတKတKပင) &ဖစ)၏။
Method Details
Method Description
PrintStream format( သတ)မ"တ)ထ+-eသ+ ပu7စ70"င)6 ပu7စ7ခ9ထ+-eသ+ String အ+-
String format,
Output Stream မ" eန၍ eဖ+)&ပ0iuင)ရန) အသu7-ဝင)eသ+
Object … args)
Method &ဖစ)ပ3သည)။

PrintStream format( သတ)မ"တ)ထ+-eသ+ Locale 0"င)6 ပu7စ7တiuအ+- အသu7-&ပuက+


Locale l,
ပu7စ7ခ9ထ+-eသ+ String အ+- Output Stream မ" eဖ+)&ပ0iuင)
String format,
ရန) အသu7-ဝင)eသ+ Method &ဖစ)ပ3သည)။
Object … args)
Sample Codes
public static void main(String[] args) {
PrintStream ps = new PrintStream(System.out);
ps.printf("My Name is %s.%n", "Min Lwin");
ps.printf("I am %d years old", 39);
}
• အထက)ပ3ကuဒ)မ9+-တ<င) %s eနရ+တ<င) String အ+- အစ+-ထiu-မည)&ဖစ)PပQ-၊ ဤနမIန+တ<င)
“Min Lwin” အ+- အစ+-ထiu-ပ3 မည)။

• တဖန) %n သည) မ9S)-တစ)eDက+င)-&ခ+-ရန) အမ"တ)အသ+-&ဖစ)PပQ- ပတ)ဝင)-က9င)အeပLမIတည)


&ခင)- မTiပ3။

• eန+က)ဆu7-တ<င) %d ၏ eနရ+တ<င) Decimal Integer အ+- အစ+-ထiu-မည)&ဖစ)PပQ-၊ ဤeနရ+


တ<င) 39 အ+- အစ+-ထiu-ပ3မည)။
Converters
Converter Description
d A decimal Integer
f A float number
n မည)သU)6 ပတ)ဝင)-က9င)မဆiu လiuင)-အသစ) တစ)eDက+င)-ကiu &ဖU)6စ<က) eပ-

မည)&ဖစ)သည)။ \n အစ+- %n အ+- အသu7-&ပuသင)6ပ3သည)။


tB Date & Time Convertion

Locale အeပLမIတည)၍ လ၏အမည)အ+- အ&ပU)6အစu7 eဖ+)&ပeပ-ပ3မည)။
td, te Date & Time Convertion

2 digit date အ+- အသu7-&ပuပ3မည)


td ဆiuပ3က စ+လu7-0"စ)လu7-မ&ပU)6သU)6 e06မ9+-၏ eVတ<င) 0 အ+- &ဖU)6စ<က)

PပQ-၊ te ဆiuပ3က 0 အ+- &ဖU)6စ<က)မည)မဟuတ)ပ3။


Converters
Converter Description
ty, tY Date & Time Convertion
ty အ"# အသu&#'ပuပ*က 2 digit year အ"#အသu&#'ပu,ပ-#၊
tY အ"#အသu&#'ပuပ*က 4 digit year အ"# အသu&#'ပuပ*မည1။
tl Date & Time Convertion

န"ရ-အ"# ၁၂န"ရ-စနစ1'ဖင1: eဖ"1'ပeပ#ပ*မည1။
tM Date & Time Convertion

မiနစ1အ"# စ"လu&#>?စ1လu&#'ဖင1:eဖ"1'ပ,ပ-#၊ လiuအပ1ပ*က 0 အ"# အစ"#ထiu#eဖ"1'ပမည1။
tp Date & Time Convertion
Locale အရ am/pm အ"# စ"လu&#အeသ#'ဖင1: eဖ"1'ပမည1။
tm Date & Time Convertion
စ"လu&#>?စ1လu&#'ဖင1: လအ"#eဖ"1,ပ-#၊ လiuအပ1ပ*က 0 အ"# အစ"#ထiu# eဖ"1'ပeပ#ပ*
မည1။
tD Date & Time Convertion

ရက1စABအ"# %tm%td%ty 'ဖင1: eဖ"1'ပeပ#ပ*မည1။
Flags
Flags Description

08 စ"လu&# ၈လu&#အ"# အသu&#'ပu၍ လiuအပ1ပ*က 0 အ"# 'ဖည1:စAက1မည1

+ အeပ*င1#အ>Eတ1လကGဏ"အ"# 'ဖည1:စAက1eပ#မည1

, Locale အရ ကiန1#မI"#အ"# အuပ1စuဖABJမည1

- ဘယ1ဘက1သiuစu၍ eဖ"1'ပမည1

.3 ဒသမကiန1#၃လu&#အ"# eဖ"1'ပမည1

စ"လu&#၁၀လu&#အ"# ည"ဘက1သiu'ဖည1:စ-က"၊ ဒသမကiန1#၃လu&#အ"# eဖ"1'ပeပ#


10.3
မည1'ဖစ1သည1။
Samples
public static void main(String[] args) {
long data = 756659;

System.out.format("%d%n", data); // "756659"


System.out.format("%08d%n", data); // "00756659"
System.out.format("%+08d%n", data);// "+0756659"

double pi = Math.PI;

System.out.format("%f%n", pi); // "3.141593"


System.out.format("%.3f%n", pi); // "3.142"
System.out.format("%10.3f%n", pi); // " 3.142"
System.out.format("%-10.3f%n", pi);// "3.142 "

Calendar c = Calendar.getInstance();
// "March 12, 2014"
System.out.format("%tB %te, %tY%n", c,c,c);
// "03/12/14"
System.out.format("%tD%n", c);
}
DecimalFormat
public static void main(String[] args) {
DecimalFormat df = new DecimalFormat("###,###,##0.000");
System.out.println(df.format(Math.sin(12)));
}

• ကiန)-ဂဏန)-မ9+-အ+- ပu7စ7သ<င)-ရ+တ<င) DecimalFormat Class ကiuလည)-


အသu7-&ပu0iuင)ပ3သည)။

• DateFormat Class ၏ Object df အ+- တည)eဆ+က)ရ+တ<င) Constructor


Parameter အeနန"င)6 ပu7စ7e&ပ+င)-လiuသU)6 Pattern အ+- eရ-သ+-ထ+-eသ+
String တစ)ခuအ+- ရယKပ3သည)။

• DecimalFormat#format အ+- eခLယK၍ Format ခ9ထ+-eသ+ String တန)ဖiu-


အ+- ရTi0iuင)မည) &ဖစ)သည)။
Beyond Fundamental
Calculation

Exponential and
Basic Methods Trigonometric
Logarithmic
Basic Methods
Method Description
double abs(double d)
 Argument ၏ Absolute Value အ+- &ပန)လည)ရTiမည)။
float abs(float f)

int abs(int i)

long abs(long l)
double ceil(double d) Argument ထက)XကQ-eသ+ သiuမဟuတ) တKညQeသ+ အeသ-ဆu7-
Integer အ+- double ပu7စ7&ဖင)6&ပန)လည)ရTiမည)။

double floor(double d) Argument ထက)ငယ)eသ+ သiuမဟuတ) တKညQeသ+ အXကQ-ဆu7-


Integer အ+- double ပu7စ70"င)6 &ပန)လည)ရTiမည)။

double rint(double d) Argument 0"င)6 အနQ-စပ)ဆu7- တန)ဖiu- အ+- double ပu7စ70"င)6


&ပန)လည)ရTiမည)။

long round(double d)
 Argumet ၏ အနQ-စပ)ဆu7-တန)ဖiu-အ+- long သiuမဟuတ) int ပu7စ70"


int round(float f) င)6 &ပန)လည)ရTiမည)။
Basic Methods
Method Description
double min(double arg1, double arg2) Argument 0"စ)ခu၏ အeသ-ငယ)ဆu7- Argument
float min(float arg1, float arg2) အ+- &ပန)လည)ရTiမည)&ဖစ)သည)။
int min(int arg1, int arg2)
long min(long arg1, arg2)

double max(double arg1, double arg2) Argument 0"စ)ခu၏ အXကQ-ဆu7- Argument အ+-
float max(float arg1, float arg2) &ပန)လည)ရTiမည) &ဖစ)သည)။
int max(int arg1, int arg2)
long max(long arg1, arg2)
Sample Codes
double a = -191.635;
double b = 43.74;
int c = 16, d = 45;

System.out.printf("The absolute value " + "of %.3f is %.3f%n",


a, Math.abs(a));

System.out.printf("The ceiling of " + "%.2f is %.0f%n",


b, Math.ceil(b));

System.out.printf("The rint of %.2f " + "is %.0f%n",


b, Math.rint(b));

System.out.printf("The max of %d and " + "%d is %d%n",


c, d, Math.max(c, d));

System.out.printf("The min of of %d " + "and %d is %d%n",


c, d, Math.min(c, d));
Exponential & Logarithmic
Methods
Method Description
double exp(double d) Argument d ၏ Exponent အ+- တ<က)ခ9က)ရ+တ<င) အသu7-&ပu
0iuင)ပ3သည)။

double log(double d) Argument ၏ log တန)ဖiu-အ+- T+eဖ<ရ+တ<င) အသu7-&ပu0iuင)


ပ3သည)။

double pow(double base, double Argument base အ+- exponent &ဖင)6 ထပ)ကiန)-တင)၍
exponent) တန)ဖiu-အ+-T+eဖ<ရ+တ<င) အသu7-&ပu0iuင)ပ3သည)။

double sqrt(double d) Argument ၏ Square Root တန)ဖiu-အ+- T+eဖ<ရ+တ<င)


အသu7-&ပu0iuင)ပ3သည)။
Sample Codes
double x = 4;
double y = 2;

System.out.printf("The value of " + "e is %.4f%n",


Math.E);

System.out.printf("exp(%.3f) " + "is %.3f%n",


x, Math.exp(x));

System.out.printf("log(%.3f) is " + "%.3f%n",


x, Math.log(x));

System.out.printf("pow(%.3f, %.3f) " + "is %.3f%n",


x, y, Math.pow(x, y));
Trigonometric methods
Method Description
double sin(double d) Argument d ၏ sine တန)ဖiu-အ+- T+eဖ<နiuင)ပ3သည)။
double cos(double d) Argument d ၏ cosine တန)ဖiu-အ+- T+eဖ<နiuင)ပ3သည)။
double tan(double d) Argument d ၏ tangent တန)ဖiu-အ+- T+eဖ<နiuင)ပ3သည)။
double asin(double d) Argument d ၏ arcsine တန)ဖiu-အ+- T+eဖ<နiuင)ပ3သည)။
double acos(double d) Argument d ၏ arccosine တန)ဖiu-အ+- T+eဖ<နiuင)ပ3သည)။
double atan(double d) Argument d ၏ arctangent တန)ဖiu-အ+- T+eဖ<နiuင)ပ3သည)။
double atan2(double y, double x) Rectangle Coordinate (x,y) မ" Polar Coordinate (r, theta)
အ&ဖစ)e&ပ+င)-က+၊ Theta ၏ တန)ဖiu-အ+- တ<က)ခ9က)0iuင)
ပ3သည)။

double toRadians(double d) ဒQဂရQမ" Raian အ&ဖစ)၎င)-၊ Radian မ" ဒQဂရQအ&ဖစ)၎င)- e&ပ+င)-


double toDegree(double d) ရ+တ<င) အသu7-&ပu0iuင)ပ3သည)။
Using Random Numbers
• Math Class တ<င) Random န7ပ3တ)မ9+-အ+- ရယK0iuင)ရန) random method
အ+- &ပင)ဆင)ထ+-ပ3သည)။

• Math ၏ random method သည) 0.0 <= Math.random() < 1.0 အDက+-Ti
Random န7ပ3တ)တစ)ခuအ+- Retun တန)ဖiu- အeနန"င)6 &ပန)eပ-0iuင)မည) &ဖစ)
ပ3သည)။

• အကယ)၍ 1 မ" 9 အDက+-Ti Random န7ပ3တ)အ+- ရယKလiuသU)6 အခ3တ<င)


Math.random() * 10 ဟu ရလဒ)အ+- 10 &ဖင)6 e&မZ+က)၍ ရယK0iuင)ပ3သည)။

• Math.random() သည) Random န7ပ3တ)တစ)ခuအ+- T+eဖ<ရ+တ<င) အသu7-ဝင)


eသ+)လည)-၊ စQ-ရQ-လiuက) Random န7ပ3တ)အ+- ရယKလiuသU)6အခ3မ9iu-တ<င)
java.util.Random Class အ+- အသu7-&ပuသင)6ပ3သည)။

You might also like