Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Random .000000000X's

  1. #1
    SheepCow is offline Member
    Join Date
    Sep 2011
    Posts
    1
    Rep Power
    0

    Default Random .000000000X's

    Alright so this program is suppose to make a chart converting Celsius to Farenheit, what I dont understand is why there are .000000X's after some of the numbers, any ideas?

    public class CelsiusFarenheitCalculator
    {
    public static void main(String[] args)
    {
    int C; //Celsius Value
    int aF; //Approximate Farenheit Value
    double F; //Farenheit Value

    System.out.print("Celsius\t Farenheit\t Approximate Farenheit");
    for(C=0;C<=100;C++)
    {
    aF = 2*C + 32;
    F = 1.8*C +32;
    System.out.println("\n "+C+"\t\t"+F+"\t\t "+aF);
    }
    }

    OUTPUT

    Celsius Farenheit Approximate Farenheit
    0 32.0 32

    1 33.8 34

    2 35.6 36

    3 37.4 38

    4 39.2 40

    5 41.0 42

    6 42.8 44

    7 44.6 46

    8 46.4 48

    9 48.2 50

    10 50.0 52

    11 51.8 54

    12 53.6 56

    13 55.400000000000006 58

    14 57.2 60

    15 59.0 62

    16 60.8 64

    17 62.6 66

    18 64.4 68

    19 66.2 70

    20 68.0 72

    21 69.80000000000001 74

    22 71.6 76

    23 73.4 78

    24 75.2 80

    25 77.0 82

    26 78.80000000000001 84

    27 80.6 86

    28 82.4 88

    29 84.2 90

    30 86.0 92

    31 87.80000000000001 94

    32 89.6 96

    33 91.4 98

    34 93.2 100

    35 95.0 102

    36 96.8 104

    37 98.60000000000001 106

    38 100.4 108

    39 102.2 110

    40 104.0 112

    41 105.8 114

    42 107.60000000000001 116

    43 109.4 118

    44 111.2 120

    45 113.0 122

    46 114.8 124

    47 116.60000000000001 126

    48 118.4 128

    49 120.2 130

    50 122.0 132

    51 123.8 134

    52 125.60000000000001 136

    53 127.4 138

    54 129.2 140

    55 131.0 142

    56 132.8 144

    57 134.60000000000002 146

    58 136.4 148

    59 138.2 150

    60 140.0 152

    61 141.8 154

    62 143.60000000000002 156

    63 145.4 158

    64 147.2 160

    65 149.0 162

    66 150.8 164

    67 152.60000000000002 166

    68 154.4 168

    69 156.2 170

    70 158.0 172

    71 159.8 174

    72 161.6 176

    73 163.4 178

    74 165.20000000000002 180

    75 167.0 182

    76 168.8 184

    77 170.6 186

    78 172.4 188

    79 174.20000000000002 190

    80 176.0 192

    81 177.8 194

    82 179.6 196

    83 181.4 198

    84 183.20000000000002 200

    85 185.0 202

    86 186.8 204

    87 188.6 206

    88 190.4 208

    89 192.20000000000002 210

    90 194.0 212

    91 195.8 214

    92 197.6 216

    93 199.4 218

    94 201.20000000000002 220

    95 203.0 222

    96 204.8 224

    97 206.6 226

    98 208.4 228

    99 210.20000000000002 230

    100 212.0 232

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Random .000000000X's

    Please read this: What every Computer Scientist Should Know about Floating Point Numbers

    The solution is to format the number String prior to displaying it, and to do this, you can use String.format(...) or DecimalFormat, or System.out.printf(...), your choice.
    sunde887 likes this.

Similar Threads

  1. Random Generation Not being random
    By Mmarzex in forum New To Java
    Replies: 8
    Last Post: 08-04-2011, 11:53 PM
  2. Replies: 14
    Last Post: 10-19-2009, 11:57 AM
  3. Replies: 8
    Last Post: 04-19-2009, 06:50 PM
  4. Using Random
    By razmyasdfg in forum CLDC and MIDP
    Replies: 1
    Last Post: 07-27-2008, 11:47 PM
  5. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 11:44 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •