mdbtools.libmdb.Money Maven / Gradle / Ivy
/*
* #%L
* Fork of MDB Tools (Java port).
* %%
* Copyright (C) 2008 - 2016 Open Microscopy Environment:
* - Board of Regents of the University of Wisconsin-Madison
* - Glencoe Software, Inc.
* - University of Dundee
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
package mdbtools.libmdb;
/*
* these routines are copied from the freetds project which does something
* very similiar
*/
public class Money
{
private static int MAXPRECISION = 20;
public static String mdb_money_to_string(MdbHandle mdb, int start)
{
String s;
int[] multiplier = new int[MAXPRECISION];
int[] temp = new int[MAXPRECISION];
int[] product = new int[MAXPRECISION];
int money;
int num_bytes = 8;
int i;
int pos;
int neg=0;
multiplier[0] = 1;
money = start;
if ((mdb.pg_buf[money+7] & 0x01) != 0)
{
/* negative number -- preform two's complement */
neg = 1;
for (i=0;i=0 &&
multiplier[top] == 0;top--);
start=0;
for (i = 0;i <= top;i++)
{
for (j = 0;j < 3;j++)
{
product[j+start] += multiplier[i] * number[j];
}
do_carry(product);
start++;
}
return 0;
}
private static int do_carry(int[] product)
{
int j;
for (j = 0;j < MAXPRECISION;j++)
{
if (product[j] > 9)
{
product[j+1] += product[j]/10;
product[j] = product[j]%10;
}
}
return 0;
}
static String array_to_string(int[] array, int scale)
{
int top, i, j;
for (top = MAXPRECISION-1; top >= 0 &&
top>scale &&
array[top] == 0;top--);
if (top == -1)
{
return "0";
}
j = 0;
char[] s = new char[100]; /** @todo find a better number */
for (i = top;i >= 0;i--)
{
if (top+1-j == scale)
s[j++]='.';
s[j++] = (char)(array[i] + '0');
}
return new String(s,0,j);
}
}