invoify icon indicating copy to clipboard operation
invoify copied to clipboard

Corrected Decimal Number Conversion and addressed bot feedback to correct code format and logic.

Open JeevanJoshi4434 opened this issue 1 year ago • 3 comments

This commit addresses an issue where decimal numbers in the Total Cost field were being incorrectly converted to words as fractions (e.g., "25/100"). The new implementation ensures that the decimal portion is displayed in a more user-friendly, currency-specific format (e.g., "Twenty-Five Cents" for USD or "Twenty-Five Paisa" for INR).

Changes made:

  1. Added currencies.json: A new file was added to @/public/assets/data/currencies.json, containing details of different currencies, including the currency name, decimal precision, and names for units before and after the decimal. This data is contains all currencies that are mentioned in Open Exchange Rates API (JSON is purely handwritten) and used to format decimal numbers accordingly.

    Example:

    {
        "INR": {
            "currency": "Indian Rupee",
            "decimals": 2,
            "beforeDecimal": "Rupee",
            "afterDecimal": "Paisa"
        },
        "USD": {
            "currency": "US Dollar",
            "decimals": 2,
            "beforeDecimal": "Dollar",
            "afterDecimal": "Cent"
        }
    }
    
  2. Updated formatPriceToString function:

    • The function was refactored to take both the price and currency as parameters.
    • It now fetches the correct currency details using the newly added fetchCurrencyDetails function.
    • The fractional part is converted into words (e.g., "Twenty-Five Cents") based on the afterDecimal value from the currency details.
    • In cases where the afterDecimal is null, the decimal is represented as a point followed by the fractional part in words (e.g., "point Forty-Three" for currencies like Albanian Lek).
    • If both integer and fractional parts are zero, the output is "Zero."
  3. Modified ChargesContext.tsx:

    • The useFormContext hook in @/contexts/ChargesContext.tsx was updated to also provide the getValues function, enabling the passing of the selected currency into the formatPriceToString function.

Benefits:

  • Improved Readability: The decimal portion of the price is now displayed in a more readable, user-friendly format with proper currency terms.
  • Currency-Specific Formatting: The changes support various currencies with specific rules for fractional parts (e.g., "Paisa" for INR, "Cents" for USD), making the output more localized and accurate.
  • Bug Fix: Resolves the issue where decimals were incorrectly displayed as fractions (e.g., "25/100").

This update enhances the Total Cost field's usability, improving the overall user experience with correct decimal number formatting based on the selected currency.

Summary by CodeRabbit

  • New Features

    • Enhanced total amount calculation by incorporating current currency context.
    • Introduced a new function to fetch currency details for accurate price formatting.
    • Added a new JSON file containing comprehensive currency data for various countries.
  • Bug Fixes

    • Improved handling of currency symbols and decimal places in price formatting.
  • Documentation

    • Updated type declarations to include CurrencyDetails for better currency handling.

JeevanJoshi4434 avatar Dec 07 '24 22:12 JeevanJoshi4434

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
invoify ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 8, 2024 8:26am

vercel[bot] avatar Dec 07 '24 22:12 vercel[bot]

@JeevanJoshi4434 is attempting to deploy a commit to the al1abb-team Team on Vercel.

A member of the Team first needs to authorize it.

vercel[bot] avatar Dec 07 '24 22:12 vercel[bot]

Walkthrough

The changes in this pull request enhance the ChargesContextProvider component by integrating a new method, getValues, from the useFormContext hook, which allows for retrieving current form values. This is particularly utilized in the updated calculateTotal function to accurately format the total amount in words based on the selected currency. Additionally, a new JSON file containing currency data is introduced, along with modifications to helper functions for better currency formatting based on this data.

Changes

File Change Summary
contexts/ChargesContext.tsx - Added getValues method to useFormContext
- Updated calculateTotal to use getValues("details.currency") for accurate currency formatting.
lib/helpers.ts - Added fetchCurrencyDetails function to retrieve currency data.
- Updated formatPriceToString to accept a currency parameter and utilize currency details for formatting.
public/assets/data/currencies.json - Introduced new JSON file containing currency data structured with ISO codes and properties for each currency.
types.ts - Added new type CurrencyDetails with properties for currency handling.

Possibly related issues

  • [BUG] - Incorrect Conversion of Decimal Numbers to Words in Total Cost: The changes address the issue of decimal portions being displayed as fractions by improving the logic for converting total costs to words based on currency-specific terms.

🐰 In a world of numbers, oh so bright,
We fix the words to bring delight.
From fractions to terms, we'll make it right,
With currencies dancing, a joyous sight!
Hopping through code, our changes take flight! 🌟


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

coderabbitai[bot] avatar Dec 07 '24 22:12 coderabbitai[bot]