Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with integer overflow #299

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -547,17 +547,32 @@ protected void onRestoreInstanceState(Parcelable state) {
updateKeypad();
}

/**
* @deprecated use link setNumber(BigInteger integerPart, BigDecimal decimalPart, Integer mCurrentSign) to
* avoid overflow issue
* @param integerPart
* @param decimalPart
* @param mCurrentSign
*/
@Deprecated
public void setNumber(Integer integerPart, Double decimalPart, Integer mCurrentSign) {
setNumber(BigInteger.valueOf(integerPart),BigDecimal.valueOf(decimalPart),mCurrentSign);
}

public void setNumber(BigInteger integerPart, BigDecimal decimalPart, Integer mCurrentSign) {
if (mCurrentSign != null) {
mSign = mCurrentSign;
} else {
mSign = SIGN_POSITIVE;
}

if (decimalPart != null) {
String decimalString = doubleToString(decimalPart);
String decimalString = decimalPart.toPlainString();
// remove "0." from the string
readAndRightDigits(TextUtils.substring(decimalString, 2, decimalString.length()));
if (decimalString.length() > 2){
decimalString = TextUtils.substring(decimalString, 2, decimalString.length());
}
readAndRightDigits(decimalString);
mInputPointer++;
mInput[mInputPointer] = CLICKED_DECIMAL;
}
Expand All @@ -575,19 +590,6 @@ private void readAndRightDigits(String digitsToRead) {
}
}

/**
* Method used to format double and avoid scientific notation x.xE-x (ex: 4.0E-4)
*
* @param value double value to format
* @return string representation of double value
*/
private String doubleToString(double value) {
// Use decimal format to avoid
DecimalFormat format = new DecimalFormat("0.0");
format.setMaximumFractionDigits(Integer.MAX_VALUE);
return format.format(value);
}


private static class SavedState extends BaseSavedState {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.codetroopers.betterpickers.numberpicker.NumberPickerDialogFragment.NumberPickerDialogHandlerV2;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Vector;

/**
Expand All @@ -28,8 +29,8 @@ public class NumberPickerBuilder {
@Deprecated
private Vector<NumberPickerDialogHandler> mNumberPickerDialogHandlers = new Vector<NumberPickerDialogHandler>();
private Vector<NumberPickerDialogHandlerV2> mNumberPickerDialogHandlersV2 = new Vector<>();
private Integer currentNumberValue;
private Double currentDecimalValue;
private BigInteger currentNumberValue;
private BigDecimal currentDecimalValue;
private Integer currentSignValue;

/**
Expand Down Expand Up @@ -79,16 +80,24 @@ public NumberPickerBuilder setReference(int reference) {

/**
* Set initial value to display
* @deprecated use etCurrentNumber(BigInteger number)
*/
@Deprecated
public NumberPickerBuilder setCurrentNumber(Integer number) {
return setCurrentNumber(BigInteger.valueOf(number));
}

/**
* Set initial value to display
*/
public NumberPickerBuilder setCurrentNumber(BigInteger number) {
if (number != null) {
if (number >= 0) {
if (number.signum() >= 0) {
this.currentSignValue = NumberPicker.SIGN_POSITIVE;
} else {
this.currentSignValue = NumberPicker.SIGN_NEGATIVE;
number = number * -1;
number = number.abs();
}

this.currentNumberValue = number;
this.currentDecimalValue = null;
}
Expand All @@ -107,8 +116,8 @@ public NumberPickerBuilder setCurrentNumber(BigDecimal number) {
number = number.abs();
}
BigDecimal[] numberInput = number.divideAndRemainder(BigDecimal.ONE);
this.currentNumberValue = numberInput[0].intValue();
this.currentDecimalValue = numberInput[1].doubleValue();
this.currentNumberValue = numberInput[0].toBigInteger();
this.currentDecimalValue = numberInput[1];
}
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public class NumberPickerDialogFragment extends DialogFragment {

private BigDecimal mMinNumber = null;
private BigDecimal mMaxNumber = null;
private Integer mCurrentNumber = null;
private Double mCurrentDecimal = null;
private BigInteger mCurrentNumber = null;
private BigDecimal mCurrentDecimal = null;
private Integer mCurrentSign = null;
private int mPlusMinusVisibility = View.VISIBLE;
private int mDecimalVisibility = View.VISIBLE;
Expand All @@ -71,8 +71,8 @@ public static NumberPickerDialogFragment newInstance(int reference,
Integer plusMinusVisibility,
Integer decimalVisibility,
String labelText,
Integer currentNumberValue,
Double currentDecimalValue,
BigInteger currentNumberValue,
BigDecimal currentDecimalValue,
Integer currentNumberSign) {
final NumberPickerDialogFragment frag = new NumberPickerDialogFragment();
Bundle args = new Bundle();
Expand All @@ -94,10 +94,10 @@ public static NumberPickerDialogFragment newInstance(int reference,
args.putString(LABEL_TEXT_KEY, labelText);
}
if (currentNumberValue != null) {
args.putInt(CURRENT_NUMBER_KEY, currentNumberValue);
args.putSerializable(CURRENT_NUMBER_KEY, currentNumberValue);
}
if (currentDecimalValue != null) {
args.putDouble(CURRENT_DECIMAL_KEY, currentDecimalValue);
args.putSerializable(CURRENT_DECIMAL_KEY, currentDecimalValue);
}
if (currentNumberSign != null) {
args.putInt(CURRENT_SIGN_KEY, currentNumberSign);
Expand Down Expand Up @@ -138,10 +138,10 @@ public void onCreate(Bundle savedInstanceState) {
mLabelText = args.getString(LABEL_TEXT_KEY);
}
if (args != null && args.containsKey(CURRENT_NUMBER_KEY)) {
mCurrentNumber = args.getInt(CURRENT_NUMBER_KEY);
mCurrentNumber = (BigInteger) args.getSerializable(CURRENT_NUMBER_KEY);
}
if (args != null && args.containsKey(CURRENT_DECIMAL_KEY)) {
mCurrentDecimal = args.getDouble(CURRENT_DECIMAL_KEY);
mCurrentDecimal = (BigDecimal) args.getSerializable(CURRENT_DECIMAL_KEY);
}
if (args != null && args.containsKey(CURRENT_SIGN_KEY)) {
mCurrentSign = args.getInt(CURRENT_SIGN_KEY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void onCreate(Bundle savedInstanceState) {

private class SampleAdapter extends BaseAdapter implements NumberPickerDialogFragment.NumberPickerDialogHandlerV2 {

private ArrayList<Integer> mNumbers;
private ArrayList<BigInteger> mNumbers;
private LayoutInflater mInflater;
private ViewHolder holder;
private NumberPickerBuilder mNumberPickerBuilder;
Expand All @@ -48,13 +48,13 @@ public SampleAdapter(Context context, FragmentManager fm) {
mInflater = LayoutInflater.from(context);

Random random = new Random();
mNumbers = new ArrayList<Integer>();
mNumbers.add(0);
mNumbers.add(1);
mNumbers.add(-1);
mNumbers = new ArrayList<BigInteger>();
mNumbers.add(BigInteger.ZERO);
mNumbers.add(BigInteger.ONE);
mNumbers.add(BigInteger.valueOf(-1));
for (int i = 1; i < 31; i++) {
Integer randomNumber = (random.nextInt(65536) - 32768);
mNumbers.add(randomNumber);
mNumbers.add(BigInteger.valueOf(randomNumber));
}

mNumberPickerBuilder = new NumberPickerBuilder()
Expand All @@ -74,7 +74,7 @@ public int getCount() {
}

@Override
public Integer getItem(int position) {
public BigInteger getItem(int position) {
return mNumbers.get(position);
}

Expand All @@ -96,8 +96,8 @@ public View getView(final int position, View convertView, ViewGroup parent) {
holder = (ViewHolder) view.getTag();
}

final Integer i = getItem(position);
holder.text.setText("" + i);
final BigInteger i = getItem(position);
holder.text.setText(i.toString());
holder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand All @@ -113,7 +113,7 @@ public void onClick(View v) {

@Override
public void onDialogNumberSet(int reference, BigInteger number, double decimal, boolean isNegative, BigDecimal fullNumber) {
mNumbers.set(reference, number.intValue());
mNumbers.set(reference, number);
notifyDataSetChanged();
}
}
Expand Down