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

Support for animating numbers with decimals #43

Open
marcfrankel opened this issue Jul 20, 2022 · 4 comments
Open

Support for animating numbers with decimals #43

marcfrankel opened this issue Jul 20, 2022 · 4 comments

Comments

@marcfrankel
Copy link

Great work on this library so far! Sadly, for our intended use case, we'd need it to support numbers with decimals. Is this something that would be possible or on your intended roadmap?

@waleedAlmubarak
Copy link

+1

@vishnummenon
Copy link

@marcfrankel @waleedAlmubarak Were you able to find a solution to animate numbers with decimals?

Thanks!

@SatyamReactNative
Copy link

+1

@drichar
Copy link

drichar commented Nov 21, 2024

Just split the number into whole and decimal parts and use two instances of AnimatedNumbers with a . between them.

I'm using this:

const getPriceParts = (price: number) => {
  const [whole, decimal = '000000'] = price.toFixed(6).split('.')
  return {
    whole: parseInt(whole),
    decimal: parseInt(decimal.padEnd(6, '0')),
  }
}

const currentPriceParts = getPriceParts(currentPrice)

<View style={styles.priceContainer}>
  <Text style={styles.priceDollarSign}>$</Text>
  <AnimatedNumbers
    includeComma
    animateToNumber={currentPriceParts.whole}
    fontStyle={styles.price}
    animationDuration={300}
  />
  <Text style={styles.priceDecimal}>.</Text>
  <AnimatedNumbers
    animateToNumber={currentPriceParts.decimal}
    fontStyle={styles.priceDecimal}
    animationDuration={300}
  />
</View>

const styles = StyleSheet.create({
  priceContainer: {
    flexDirection: 'row',
    alignItems: 'center',
    marginBottom: 2,
  },
  priceDollarSign: {
    fontSize: 36,
    fontWeight: '600',
    color: '#000',
  },
  price: {
    fontSize: 36,
    fontWeight: '600',
    color: '#000',
  },
  priceDecimal: {
    fontSize: 36,
    fontWeight: '600',
    color: '#000',
  },
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants