• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Given a float number convert it into the string WITHOUT using any inbuilt Function

March 25, 2014 by Dhaval Dave

Convert a floating point number to string

Given a float number convert it into the string WITHOUT using any inbuilt Function

Method 1:

class Float2String 
{ 
   public static void main(String[] args) 
   { 
      float f=7.64f; 
      String result; 
      result=""+f; 
      System.out.println(result); 
   } 
}


-by karthik dheeraj

Method 2:

#include<stdio.h>
#include<math.h>
 
// reverses a string 'str' of length 'len'
void reverse(char *str, int len)
{
    int i=0, j=len-1, temp;
    while (i<j)
    {
        temp = str[i];
        str[i] = str[j];
        str[j] = temp;
        i++; j--;
    }
}
 
 // Converts a given integer x to string str[]. 
int intToStr(int x, char str[], int d)
{
    int i = 0;
    while (x)
    {
        str[i++] = (x%10) + '0';
        x = x/10;
    }
 
    while (i < d)
        str[i++] = '0';
 
    reverse(str, i);
    str[i] = '\0';
    return i;
}
 
// Converts a floating point number to string.
void ftoa(float n, char *res, int afterpoint)
{
    int ipart = (int)n;
 
    float fpart = n - (float)ipart;
 
    int i = intToStr(ipart, res, 0);
 
    if (afterpoint != 0)
    {
        res[i] = '.';  // add dot
 
        // Get the value of fraction part upto given no.
        fpart = fpart * pow(10, afterpoint);
 
        intToStr((int)fpart, res + i + 1, afterpoint);
    }
}
 
int main()
{
    char res[20];
    float n = 233.007;
    ftoa(n, res, 4);
    printf("\n\"%s\"\n", res);
    return 0;
}

Similar Articles

Filed Under: Amazon Interview Question, problem Tagged With: string

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Print all nodes that are at distance k from a leaf node

Given array of 0’s and 1’s. All 0’s are coming first followed by 1’s. find the position of first 1

There are N nuts and N bolts, u have to find all the pairs of nuts and bolts in minimum no. of iteration

‘N’ Story Building, with 1,2,3 steps how many ways can a person reach top of building.

Reliance Jio Software Developer Interview Experience

Find if two rectangles overlap

LeetCode : Word Search

Puzzle : 100 doors in a row Visit and Toggle the door. What state the door will be after nth pass ?

DFS (Depth First Search)

Trie Dictionary

In Given LinkedList Divide LL in N Sub parts and delete first K nodes of each part

Subset Sum Problem Dynamic programming

Linked List V/S Binary Search Tree

robot standing at first cell of an M*N matrix. It can move only in two directions, right and down. In how many ways, it can reach to the last cell i.e. (M, N) Code it

Connect n ropes with minimum cost

FizzBuzz Solution C C++

Given a sorted array and a number x, find the pair in array whose sum is closest to x

Length of the longest substring without repeating characters

Find the smallest window in a string containing all characters of another string

Flipkart Set 1 On Campus with Answers

Possible sizes of bus to carry n groups of friends

BlueStone E-commerce Interview Experience

Maximum size of square sub matrix with all 1’s in a binary matrix

Maximum difference between two elements s.t larger element appears after the smaller number

Implement a generic binary search algorithm for Integer Double String etc

Spanning Tree

C++ OOPs Part2

Sequence Finder Dynamic Programming

Flipkart SDET Interview Experience

Circular Linked List

Copyright © 2025 · Genesis Framework · WordPress · Log in