| 32 | return roundHalfAwayFromZero(parseFloat(value)); |
| 33 | } |
| 34 | |
| 35 | // ABAP rounds a half away from zero when a float is moved to an integer, |
| 36 | // on both sides: 0.5 is 1 and -0.5 is -1. Math.round rounds a half towards |
| 37 | // positive infinity, so -0.5 was 0 (and -0 at that) |
| 38 | export function roundHalfAwayFromZero(value: number): number { |
| 39 | const rounded = value < 0 ? -Math.round(-value) : Math.round(value); |
| 40 | return rounded === 0 ? 0 : rounded; |
| 41 | } |
| 42 | |
| 43 | export class Integer implements INumeric { |
| 44 | private value: number; |
| 45 | private constant: boolean = false; |
| 46 | private integerCalculationType = true; |
| 47 | private readonly qualifiedName: string | undefined; |
| 48 | |
| 49 | public constructor(input?: {qualifiedName?: string}) { |
| 50 | this.value = 0; |
| 51 | this.qualifiedName = input?.qualifiedName; |
| 52 | } |
| 53 | |
| 54 | /** ABAP determines the calculation type from the operand types, character-like operands raise it |
| 55 | * above type i even if the intermediate result is integer, ie. "'1.0' * 18 / 16" is not rounded */ |
| 56 | public clearIntegerCalculationType(): Integer { |
| 57 | this.integerCalculationType = false; |
| 58 | return this; |
| 59 | } |
| 60 | |
| 61 | public isIntegerCalculationType(): boolean { |
| 62 | return this.integerCalculationType; |
| 63 | } |
| 64 | |
| 65 | public getQualifiedName() { |
| 66 | return this.qualifiedName; |
| 67 | } |
| 68 | |
| 69 | public clone(): Integer { |
| 70 | const n = new Integer({qualifiedName: this.qualifiedName}); |
| 71 | // set without trigger checks and padding |
| 72 | n.value = this.value; |
| 73 | return n; |
| 74 | } |
| 75 | |
| 76 | public setConstant() { |
| 77 | this.constant = true; |
| 78 | return this; |
| 79 | } |
| 80 | |
| 81 | public set(value: INumeric | ICharacter | Hex | string | number | Integer | Float | DecFloat34) { |
| 82 | if (this.constant === true) { |
| 83 | throw new Error("Changing constant"); |
| 84 | } |
| 85 | |
| 86 | if (typeof value === "number") { |
| 87 | this.value = roundHalfAwayFromZero(value); |
| 88 | } else if (value instanceof Integer) { |
| 89 | this.set(value.get()); |
| 90 | } else if (value instanceof Character) { |
| 91 | this.value = toInteger(value.get()); |
nothing calls this directly
no outgoing calls
no test coverage detected