Open In App

Convert String to currency format

Improve
Improve
Like Article
Like
Save
Share
Report

Given a number N. Convert N into an Indian currency format. For more understanding, please look into examples.

Examples:

Input: N = 1000000
Output: Rs 10, 00, 000

Input: N = 1500
Output: Rs 1, 500

Approach: Steps involved in the implementation of code:

  • We need to check whether the length of the string is even or odd.
  • If the length of the string is less than equal to 3 we will simply return it else we will do the following, newString = “”.
  • if the length is odd:
    • we will the first character into a new string newSstring = N[0].
    • and then we will and commas new string = “, “.
  • Now we will skip the two characters and then add “, ” till the length < n-2.
  • And at last, we will add the remaining characters to the newString.

Below is the implementation of the code:

C++

// C++ implementation of the code
#include <bits/stdc++.h>
using namespace std;

// Function to convert N
// into indian currency
string goodFormat(string s, int n)
{

    // If length if less than 3
    if (n <= 3)
        return "Rs. " + s;

    string ans = "";
    int start = 0, cnt = 0;

    // If length is even
    if (n % 2 == 0) {
        ans += s[0];
        ans += ", ";
        start = 1;
    }
    while (start < n - 2) {
        if (cnt == 2) {
            ans += ", ";
            cnt = 0;
            continue;
        }
        else {
            ans += s[start];
            cnt++;
        }
        start++;
    }
    for (int i = start; i < n; i++)
        ans += s[i];

    return "Rs " + ans;
}

// Drivers code
int main()
{
    string s = "1000000";
    int l = s.length();

    // Function Call
    cout << goodFormat(s, l);
    return 0;
}

Java

// Java implementation of the code
import java.util.*;
class GFG {

  // Function to convert N
  // into Indian currency
  static String goodFormat(String s, int n)
  {

    // If length is less than 3
    if (n <= 3)
      return "Rs. " + s;

    String ans = "";
    int start = 0, cnt = 0;

    // If length is even
    if (n % 2 == 0) {
      ans += s.charAt(0);
      ans += ", ";
      start = 1;
    }

    while (start < n - 2) {
      if (cnt == 2) {
        ans += ", ";
        cnt = 0;
        continue;
      }
      else {
        ans += s.charAt(start);
        cnt++;
      }
      start++;
    }

    for (int i = start; i < n; i++)
      ans += s.charAt(i);

    return "Rs " + ans;
  }

  // Drivers code
  public static void main(String[] args)
  {
    String s = "1000000";
    int l = s.length();

    // Function Call
    System.out.println(goodFormat(s, l));
  }
}

// This code is contributed by prasad264

Python3

# Python3 implementation of the code

# Function to convert N
# into indian currency
def goodFormat(s, n):
  # If length if less than 3
  if (n <= 3):
      return "Rs. " + s

  ans = ""
  start = 0
  cnt = 0

  # If length is even
  if (n % 2 == 0):
      ans += s[0]
      ans += ", "
      start = 1

  while (start < n - 2):
      if (cnt == 2):
          ans += ", "
          cnt = 0
          continue
      else:
          ans += s[start]
          cnt += 1
      start += 1

  for i in range(start, n):
      ans += s[i]

  return "Rs " + ans

# Drivers code
s = "1000000"
l = len(s)

# Function Call
print(goodFormat(s, l))

C#

using System;

public class IndianCurrencyFormatter
{
    public static string GoodFormat(string s)
    {
        int n = s.Length;

        // If length if less than 3
        if (n <= 3)
            return "Rs. " + s;

        string ans = "";
        int start = 0, cnt = 0;

        // If length is even
        if (n % 2 == 0)
        {
            ans += s[0];
            ans += ", ";
            start = 1;
        }

        while (start < n - 2)
        {
            if (cnt == 2)
            {
                ans += ", ";
                cnt = 0;
                continue;
            }
            else
            {
                ans += s[start];
                cnt++;
            }
            start++;
        }

        for (int i = start; i < n; i++)
            ans += s[i];

        return "Rs " + ans;
    }

    public static void Main()
    {
        string s = "1000000";

        // Function Call
        Console.WriteLine(GoodFormat(s));
    }
}

Javascript

// Javascript implementation of the code

// Function to convert N
// into indian currency
function goodFormat(s, n)
{
    // If length if less than 3
    if (n <= 3)
        return "Rs. " + s;

    let ans = "";
    let start = 0, cnt = 0;

    // If length is even
    if (n % 2 == 0) {
        ans += s[0];
        ans += ", ";
        start = 1;
    }
    while (start < n - 2) {
        if (cnt == 2) {
            ans += ", ";
            cnt = 0;
            continue;
        }
        else {
            ans += s[start];
            cnt++;
        }
        start++;
    }

    for (let i = start; i < n; i++)
        ans += s[i];

    return "Rs " + ans;
}

// Drivers code
let s = "1000000";
let l = s.length;

// Function Call
console.log(goodFormat(s, l));
Output

Rs 10, 00, 000

Time Complexity: O(N)
Auxiliary Space: O(1)


Last Updated : 16 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads