aspose file tools*
The moose likes Beginning Java and the fly likes mod using double Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "mod using double" Watch "mod using double" New topic
Author

mod using double

praveen mars
Greenhorn

Joined: Feb 06, 2003
Posts: 3
Hi All,
If i am using and expression like:-
number % 100D
where number is a double , it give an error saying
possible loss of precision
found : double
required: int
I understand double is not a whole number, but if i want to use an expression like above how to use it?
I will appreciate ur help in this .
ok the code is like this :--
------************************************--------------------
import java.io.*;
import pernec.global.util.*;

public class SpellinWordsForDouble{

static String sNumber = "";
static String prefix = "";
static String soFar = "";

private static void main(String [] args) throws IOException {
try
{
InputStreamReader isr= new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.print("Enter a number: ");
sNumber = br.readLine();
double iNumber = Double.valueOf(sNumber).doubleValue();
convert(iNumber);
System.out.println((convert(iNumber)).toString());
}
catch(Exception ee)
{
ee.printStackTrace();
}
}
/*
public static void main (String [] args) throws IOException
{
for (int i = 0; i < 99999; i++)
{

String s = convert(i);
System.out.println(i + ":" + s);
}
}*/
private static final String[] majorNames =
{
""," thousand",
" million"," billion",
" trillion"," quadrillion",
" quintillion"
};
private static final String[] tensNames =
{
""," ten",
" twenty"," thirty",
" fourty"," fifty",
" sixty"," seventy",
" eighty"," ninety"
};
private static final String[] numNames =
{
""," one",
" two"," three",
" four"," five",
" six"," seven",
" eight"," nine",
" ten"," eleven",
" twelve"," thirteen",
" fourteen"," fifteen",
" sixteen"," seventeen",
" eighteen"," nineteen"
};
private static void convertLessThanOneThousand(double number, StringBuffer buff)
{
if (((double)number % 100.0) < 20.0)
{
buff.append(""+numNames[(double)(number % 100.0)]);
number /= 100.0;
}
else
{
buff.append(""+numNames[(double)number % 10.0]);
number /= 10.0;
buff.insert(0, tensNames[(double)number % 10.0]);
number /= 10.0;
}
if (number != 0.0)
{
buff.insert(0, " hundred");
buff.insert(0, numNames[number]);
}

}
public static String convert(double number)
{
/* special case */
if (number == 0.0)
{
System.out.println ("zero");
}
String prefix = "";
StringBuffer buff1 = new StringBuffer();
StringBuffer buff2 = new StringBuffer();
if (number < 0.0)
{
number = -number;
prefix = "negative";
}
int place = 0;

do
{
double n = (double)(number % 1000.0);
if (n != 0.0)
{
buff1.insert(0, majorNames[place]);
convertLessThanOneThousand(n, buff2);
buff1.insert(0, buff2);
buff2.setLength(0);
}
place++;
number /= 1000.0;
} while (number > 0.0);

buff1.insert(0, prefix);
System.out.println("the number in words is--:"+buff1.toString());
return buff1.toString().trim();
}
}
------------------**************************-----------------
the code between the lines ----****---- can be copied and when execute will
give an error at lines 74,79,....

thanks and best regards.
[ August 13, 2003: Message edited by: praveen mars ]

pravmars
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Hi,
Welcome to JavaRanch!
The expression number % 100d is OK, but its value is of type double; you're probably getting the error because you're assigning the result to an int, or
passing the expression to a method that wants an int argument. In either case, you have to cast the expression to the appropriate type first:

OK?


[Jess in Action][AskingGoodQuestions]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
I cannot reproduce your problem. My similar code works just fine.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: mod using double