Bloomberg LP Interview Question for Financial Software Developers






Comment hidden because of low score. Click to expand.
0
of 0 vote

this is the solution not only for the intergers but also for double values (using cents)

public class ConvertDolar {
    public static void main(String[] args) {
        double amount = 1234567.99222229292929d;
        System.out.println(prinDollar(amount));
    }

    static String prinDollar(double number) {
        int cents = (int) (number % 1 * 100); // saving the cents
        int num = (int) number; // eliminating the cents
        StringBuffer dollar = new StringBuffer();
        int count = 0;
        while (num > 0) {
            if (count == 3) {
                dollar.append(",");
                count = 1;
            } else {
                count++;
            }
            dollar.append(num % 10);
            num /= 10;
        }
        return "$" + dollar.reverse().toString() + "." + (cents < 10 ? "0" + cents : cents);
    }

}

Result:

$1,234,567.99

- wsoethe March 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

And here is one using the STL:

#include <iostream>
#include <sstream>
#include <string>
#include <locale>

using namespace std;

void to_dollar(int a);

int main()
{
    int userInput;
	
    cout << "Please enter an integer-:> " << endl;
	cin >> userInput;
	to_dollar(userInput);

    return (0);
}

void to_dollar(int a)
{
    stringstream inString;
	string strTemp;
    long double intTemp;
	
	inString << a;                      // convert integer to string for easier handling
	inString.str(inString.str()+"00");  // concatenate two zeos to denote currency fractional digits
	inString >> intTemp;                // convert back to integer
	
    locale loc("english_USA");          // set the locale
	cout.imbue(loc);
	
    cout.setf(cout.flags() | ios::showbase); // turn on currency sign printing
	const money_put<char>& m_put = use_facet<money_put<char> >(loc); // get the money_put facet
    m_put.put(cout, false, cout, ' ', intTemp); // show me the money. Note to use fractionally adjusted value.
    cout << endl;
}

- intelliplay March 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Oh, I seem to have messed up the spacing. Hope you can still read the code. This is my first post so...

Let me know your comments. With the STL, the solution is really simple. Just a matter of knowing which library objects/functions to call.

- intelliplay2000 March 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int my_number_format(int num)
{
int idx;
char buffer[64];

idx = sizeof(buffer)-1;
buffer[idx--] = 0; // end zero

int count = 0;
while (num > 0) {
if (0 == idx)
return -1;
if (count > 2) {
buffer[idx--] = ',';
if (0 == idx)
return -1;
count = 0;
}
count++;
buffer[idx--] = '0'+(num % 10);
num /= 10;
}

buffer[idx] = '$'; // currency sign
return printf("%s\n", &buffer[idx]);
}

- unknown April 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We may code it using stack

void _dollar(int x)
{
char s[64];
stack stk;

*s = '\0';

while(x/1000 > 0)
{
if(!stk.isFull())
stk.push(x%1000);

x = x/1000;
}
if(!stk.isFull())
stk.push(x);

while(!stk.isEmpty())
{
sprintf(s, "%s%d,", s, stk.pop());
}

int i = 0;
while(s[i++]!='\0');


*(s+i-2) = '$';

printf(s);
}

int main()
{
_dollar(12800900);
return 0;
}

- dullboy April 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Another simpler way to do it.

char* to_dollar(int x)
{
static char s[100];
int size = sizeof(s)/sizeof(s[0]);
int index = 0;
int index2 = 0;

s[size-1] = '\0';
s[size-2] = '$';
while(x>0)
{
if(index2==3)
{
s[size-index-3] = ',';
index2 = 0;
}
else
{
s[size-index-3] = x%10+'0';
x = x/10;
index2++;
}

index++;
}

return s+size-index-2;
}

- dullboy June 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void To_Dollar(int n){
stringstream ss1,ss2;
ss1<<n;
string str1=ss1.str();
int j=0;
for(int i=static_cast<int>(str1.length()-1);i>=0;i--){

ss2<<str1[i];
j++;
if(j%3==0)ss2<<',';
}
ss2<<'$';
string str2=ss2.str();

for(int i=static_cast<int>(str2.length()-1);i>=0;i--)
cout<<str2[i];


}

- Anonymous June 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <sstream>
#include <string.h>

using namespace std;

void reverseString(char* str)
{
  char tmp;
  int len = strlen(str);
  for(int i = 0; i <= len/2; ++i)
    {
      tmp = str[i];
      str[i] = str[len-1-i];
      str[len-1-i] = tmp;
    }
}

char* to_dollar(int a)
{
  stringstream in;
  in << a;
  char* str = const_cast<char*>(in.str().c_str());
  int len = strlen(str);
  char* ret_str = new char[1+len+len/3+1];

  int count = 0;
  int j = 1;
  for(int i = len-1; i >= 0; --i)
    {
      ret_str[count] = str[i];
      if(j % 3 == 0)
        ret_str[++count] = ',';
      count++;
      j++;
    }
  ret_str[count] = '$';

  reverseString(ret_str);
  ret_str[count+1] = '\0';
  return ret_str;
  
}

int main()
{
  //00201
  int i = 10200;
  char* str = to_dollar(i);
  cout << str << endl;
  return 0;
}

- dellaxim July 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

string to_dollar(int amount){
    stringstream ss;
    ss << amount;
    string result;
    ss >> result;
    int count = (result.size()%3 ==0)? result.size()/3-1: result.size()/3 ;
    int counter = 0;
    for( int i = 1; i <= count; i++){
       result.insert(result.size()-i*3 - counter, ",");
       counter++;
    }
    result.insert(0,"$");
    return result;
}

- xicheng March 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

std::string to_dollar(const int amt) {
	std::deque<std::string> csRslt;

	int rVal = amt;
	while (rVal != 0) {
		int cVal = rVal % 1000;
		rVal /= 1000;

		std::stringstream ss;
		ss << cVal;

		csRslt.push_front(ss.str());
	}

	std::string csAmnt("$");
	for (size_t i = 0; i < csRslt.size(); ++i) {
		csAmnt += csRslt[i];
		if (i != (csRslt.size()-1)) csAmnt += ",";
	}

	return csAmnt;
}

- Passerby_A March 16, 2014 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More