Skip to content

Commit

Permalink
Move roundTo prop to CalculatorCommonProps
Browse files Browse the repository at this point in the history
  • Loading branch information
budiadiono committed Dec 8, 2018
1 parent fbe3786 commit 23a541b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ All props in [common props](#common-props) and...
| onTextChange | (text: string) => void | | Text change event. |
| displayTextAlign | `"auto" / "left" / "right" / "center" / "justify"` | `"left"` | Digit align display. |
| noDecimal | boolean | false | Hide decimal separator button to disable decimal value. |
| roundTo | number | 2 | How many decimal places to round the value |

## License

Expand Down
24 changes: 13 additions & 11 deletions src/Calculator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ export interface CalculatorProps extends CalculatorCommonProps {
*/
hasAcceptButton?: boolean

/**
* How many decimal places to round the value
*/
roundTo?: number

/**
* Container style.
*/
Expand Down Expand Up @@ -327,7 +322,10 @@ export class Calculator extends React.Component<CalculatorProps, State> {
}
stack.trailing = decimalSeparator
} else if (value === '0' || value === '000') {
if (stack.value.indexOf(decimalSeparator as string) > -1 || stack.trailing !== '') {
if (
stack.value.indexOf(decimalSeparator as string) > -1 ||
stack.trailing !== ''
) {
stack.trailing = stack.trailing + value
value = ''
}
Expand All @@ -339,7 +337,9 @@ export class Calculator extends React.Component<CalculatorProps, State> {
}

// get editing value
const val = parseFloat((stack.value + value).replace(decimalSeparator as string, '.'))
const val = parseFloat(
(stack.value + value).replace(decimalSeparator as string, '.')
)

// modify current stack
stack.value = val.toString()
Expand Down Expand Up @@ -410,7 +410,7 @@ export class Calculator extends React.Component<CalculatorProps, State> {
this.popStack()
} else {
let { value, trailing } = stack
const { decimalSeparator } = this.props;
const { decimalSeparator } = this.props

if (
!value ||
Expand Down Expand Up @@ -440,14 +440,16 @@ export class Calculator extends React.Component<CalculatorProps, State> {
trailing = trailing + '0'
}

// keep decimal separator displayed
// keep decimal separator displayed
let sep = ''
if (value[value.length - 1] === '.') {
sep = this.props.decimalSeparator as string
}

// get editing value
const val = parseFloat(value.replace(decimalSeparator as string, '.'))
const val = parseFloat(
value.replace(decimalSeparator as string, '.')
)

stack.value = val.toString()
stack.text = this.format(val)
Expand Down Expand Up @@ -484,7 +486,7 @@ export class Calculator extends React.Component<CalculatorProps, State> {

// tslint:disable-next-line:no-eval
const num = eval(this.stacks.map(x => x.value).join('') || '0')
const value = Math.round(num * (10 ** roundTo)) / (10 ** roundTo)
const value = Math.round(num * 10 ** roundTo) / 10 ** roundTo
const text = this.format(value)

this.stacks = [
Expand Down
5 changes: 5 additions & 0 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ export interface CalculatorCommonProps {
* Hide decimal separator button to disable decimal value.
*/
noDecimal?: boolean

/**
* How many decimal places to round the value
*/
roundTo?: number
}

export const DefaultCommonProps: Partial<CalculatorCommonProps> = {
Expand Down

0 comments on commit 23a541b

Please sign in to comment.