Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › help with 2 decimal places...
New Posts  All Forums:Forum Nav:

help with 2 decimal places...

post #1 of 8
Thread Starter 
basically, I have declared the instance variable 'GPA' which is to divide 2 other instance variables, 'hours_taken' and 'grade points'. This is done using a toString method so that I can call it later in another class. All of the GPAs I'm trying to calculate are 2.something, but when the program compiles, it ignores the remainder and prints 2.0. Here's the code.

Code:
public String toString ()//This is the value to be returned in the Main class.
    {
                return("Student Name: " + student_name + "\
Classification: "
                        + classification + "\
"
                        + "Hours taken: "+ hours + "\
Grade Point: "
                        + grade_point+"\
GPA: "+GPA);
             
    }//End toString
How do I format GPA to show 2.## instead of having it ditch the remainder and printing 2.0. Thanks
post #2 of 8
Are 'hours_taken' and 'grade_points' ints? You probably just need to cast both (maybe just one?) to a double when calculating 'GPA'.
post #3 of 8
Quote:
Originally Posted by t-ramp View Post
Are 'hours_taken' and 'grade_points' ints? You probably just need to cast both (maybe just one?) to a double when calculating 'GPA'.
Yup, check the variable type to ensure everything is a float.
Once again...
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 [4.28GHz, HT] Asus P6T + Broadcom NetXtreme II VisionTek HD5850 [900/1200] + Galaxy GT240 2x4GB G.Skill Ripjaw X [1632 MHz] 
Hard DriveOSMonitorKeyboard
Intel X25-M 160GB + 3xRAID0 500GB 7200.12 Window 7 Pro 64 Acer H243H + Samsung 226BW XARMOR-U9BL  
PowerCaseMouseMouse Pad
Antec Truepower New 750W Li Lian PC-V2100 [10x120mm fans] Logitech G9 X-Trac Pro 
  hide details  
Reply
Once again...
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 [4.28GHz, HT] Asus P6T + Broadcom NetXtreme II VisionTek HD5850 [900/1200] + Galaxy GT240 2x4GB G.Skill Ripjaw X [1632 MHz] 
Hard DriveOSMonitorKeyboard
Intel X25-M 160GB + 3xRAID0 500GB 7200.12 Window 7 Pro 64 Acer H243H + Samsung 226BW XARMOR-U9BL  
PowerCaseMouseMouse Pad
Antec Truepower New 750W Li Lian PC-V2100 [10x120mm fans] Logitech G9 X-Trac Pro 
  hide details  
Reply
post #4 of 8
Quote:
Originally Posted by unfbilly11 View Post
basically, I have declared the instance variable 'GPA' which is to divide 2 other instance variables, 'hours_taken' and 'grade points'. This is done using a toString method so that I can call it later in another class. All of the GPAs I'm trying to calculate are 2.something, but when the program compiles, it ignores the remainder and prints 2.0. Here's the code.

Code:
public String toString ()//This is the value to be returned in the Main class.
    {
                return("Student Name: " + student_name + "\
Classification: "
                        + classification + "\
"
                        + "Hours taken: "+ hours + "\
Grade Point: "
                        + grade_point+"\
GPA: "+GPA);
             
    }//End toString
How do I format GPA to show 2.## instead of having it ditch the remainder and printing 2.0. Thanks
You need to provide your initialization or full code...because we dont know what your variables are.

If you want to truncate a float or double to 2 decimal places...then do something like this when printing to the screen.

Code:
System.out.format(%.2f , variable_name);           //for a double

Edited by surfbumb - 7/27/11 at 9:30am
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
post #5 of 8
Thread Starter 
Yeah that's exactly what it was. Such a stupid problem. duh. thanks everybody
post #6 of 8
Thread Starter 
one more question.... when I declare one of the other instance variables a double, it will print out '2.###########'. how do I write the code to print out '2.##'?
post #7 of 8
DoubleVariable.ToString("0.00")
Main System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II X6 1090T MSI 890FXA-GD70 XFX Radeon 5850 16 GB Corsair XMS3 DDR3 1333 
Hard DriveOSMonitorPower
3 X 7200 RPM / 1 TB Win 7 Ultimate 64 2 x ASUS 23" VH232H 1080P SeaSonic X-650 
Case
Corsair 600T 
  hide details  
Reply
Main System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II X6 1090T MSI 890FXA-GD70 XFX Radeon 5850 16 GB Corsair XMS3 DDR3 1333 
Hard DriveOSMonitorPower
3 X 7200 RPM / 1 TB Win 7 Ultimate 64 2 x ASUS 23" VH232H 1080P SeaSonic X-650 
Case
Corsair 600T 
  hide details  
Reply
post #8 of 8
Quote:
Originally Posted by unfbilly11 View Post
one more question.... when I declare one of the other instance variables a double, it will print out '2.###########'. how do I write the code to print out '2.##'?
Code:
System.out.format(%.2f, pi);        //3.14
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › help with 2 decimal places...