🔒 Closed How to limit decimal places on java

Status
Not open for further replies.

Princess Serrano

Honorary Poster
This is my first time in the field of programming and we have discussing now about java. Kung sino po may alam ng mga sumusunod pa pm nalang po.

How to use square root and power in java
How to limit decimal places in double

Yun lang po muna. Salamat!
 
Pwede ka rin po gumamit ng DecimalFormat class to format kung how many decimal places ang gusto mo after the decimal point.
(You do not have permission to view the full content of this post. Log in or register now.)

double number = 0.9999999999999;
DecimalFormat numberFormat = new DecimalFormat("#.00");
System.out.println(numberFormat.format(number));

Tulad ng sabi ni boss FAYC and 999 others :) for squareroot and power meron po Math class para doon.

Math.pow(2,3)
Math.sqrt(4)

Tawagin nalang po kapag kailangan mo siya...
 
Pwede ka rin po gumamit ng DecimalFormat class to format kung how many decimal places ang gusto mo after the decimal point.
(You do not have permission to view the full content of this post. Log in or register now.)

double number = 0.9999999999999;
DecimalFormat numberFormat = new DecimalFormat("#.00");
System.out.println(numberFormat.format(number));

Tulad ng sabi ni boss FAYC and 999 others :) for squareroot and power meron po Math class para doon.

Math.pow(2,3)
Math.sqrt(4)

Tawagin nalang po kapag kailangan mo siya...
Wow hinay hinay lang po kuya. Hahaha.
Ano po yung format?
 
What is looping structures po?
looping structure examples:

while loop, for loop, foreach loop, do while loop... bale ang gamit nila is irarun nila ang code mo as long as na ang condition na tinetest po niya is always "true" :)
(You do not have permission to view the full content of this post. Log in or register now.)

simple example of while loop:

int x = 1; // x = any variable name po na gusto mo..

while(x < 5) {
System.out.println(x); // bale magpeprint po siya ng 1,2,3,4 in that order po since 1 - 4 is less than 5.
x++; // then increment natin ang ating variable "x" ng isa para hindi po magrun ang code in a continues loop ...
}
// since nagaadd po tayo ng one sa ating variable "x" ng 1 every loop po niya sa code darating po tayo sa point na hindi na po magiging less than ang ating variable "x" sa 5 (5<5 = false) so bale mageend na po yung loop natin. so magpoproceed na po ang program sa next line. Para kahit papaano po ay makamove on na tayo ... :)
 
Bakit may space yung decimal format at number format? Pati paano maaapply yun sa number. As i said im just a beginner sensya na po.
DecimalFormat = ay isa po ay java class Object(You do not have permission to view the full content of this post. Log in or register now.)
numberFormat = is just a variable name po.. pwede po kahit anong variable name po iassign mo po wala pong problema basta sunod lang sa rules..

Example:
double number = 0.99999; // variable declaration (You do not have permission to view the full content of this post. Log in or register now.)

kailangan pong may space para po madestinguish kung saan ang Object at ang variable name. (refer above).

meron pa rin pong sinusunod na format po sa paginstantiate ng object po.. :)(You do not have permission to view the full content of this post. Log in or register now.)

Systax: <Objectname>space|| <Variablename> = new <Objectname>();
Example: DecimalFormat numberFormat = new DecimalFormat("#.00"); // bale ang ginawa po natin dito ay nagcreate po tayo ng DecimalFormat Object at naglagay po tayo ng format|template ng decimal number as #.00

so para po gamitin yung ay tawagin lang natin ang ating ginawa na object na numberFormat then call ang kanyang "format" method..

numberFormat.format(<then dito po natin ilalagay ang double value na ifoformat po natin base sa template>)....
numberFormat.format(number)

then kapag ipeprint na po natin
> System.out.println(numberFormat.format(number)); // ang output po niya is instead na "0.99999" ay "0.99"
nalang since only 2 decimal digits lang naman ang kukunin natin base po sa template na binigay natin na "#.00" :)
 
Status
Not open for further replies.

About this Thread

  • 8
    Replies
  • 1K
    Views
  • 3
    Participants
Last reply from:
JustFriends

Trending Topics

Online now

Members online
340
Guests online
1,712
Total visitors
2,052

Forum statistics

Threads
2,285,392
Posts
29,029,733
Members
1,219,428
Latest member
gh2025
Back
Top