-6

need an programming logic to print an 4 decimal points

  EX: scalar should be 0 to -5   
  value = 10006 , scalar = -3 then print result = 10.0060 (4 decimals)  
  value = 123   ,scalar = -5  then print result = 0.0012 (4 decimals)**  
  required value/divisor = 10 , value%divisor = 0060 (required logic after decimals )

I tried like this:

 divisor =  std::pow(10,std::abs(scalar));

 **Result = snprintf(X,Y,"%d.%0*d",value/scalar,4,value%scalar);**

I'm not allowed to use float , setprecision() .

It does not necessarily represent the actual value , but we can format that value to print with logic like the original one (by using the logic , add ...subtract...pow etc)

std::int32_t divisor = static_cast(std::pow( 10.0F, std::abs( Scalar)) );

but int the above result modulus scalar value with 0 are not considering. **Please provide me the logic to print the above result with scalar condition

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
1User
  • 614
  • 3
  • 13
  • Almost duplicate of [How to 'cout' the correct number of decimal places of a double value?](https://stackoverflow.com/questions/4217510/how-to-cout-the-correct-number-of-decimal-places-of-a-double-value) – user4581301 Jul 05 '17 at 19:54
  • @user4581301 - i required that using printf , not by using any float , setprecision or %C – 1User Jul 05 '17 at 19:56
  • You should be able to search the internet for [`printf` format specifiers](http://en.cppreference.com/w/cpp/io/c/fprintf), or any good C or C++ reference. – Thomas Matthews Jul 05 '17 at 20:01
  • 1
    Does your "Note" mean: "I haven't used [these things]", or "I'm not allowed to use [these things]"? – Jerry Coffin Jul 05 '17 at 20:01
  • @JerryCoffin - iam not allowed to use that float or setprecision() – 1User Jul 05 '17 at 20:05
  • What's your purpose in not using floating point? School assignment? Online Judge? Most real life programming allows for division and floating point. There are exceptions for constrained systems. – Thomas Matthews Jul 05 '17 at 20:08
  • You probably want to clarify the requirements in the question, not just a comment. – Jerry Coffin Jul 05 '17 at 20:10
  • 1
    Recommendation: watch out for `std::pow` it delves into floating point calculations deep inside and may result in a number like 9999.9999999 being returned in place of 10000. 9999.9999999 will then be truncated to 9999 and produce the wrong answer. When you are working in integers it's usually safer just to perform the multiplications yourself. In this case you could use a look-up table or a `switch`. – user4581301 Jul 05 '17 at 20:14
  • 1
    Possible duplicate of [How to 'cout' the correct number of decimal places of a double value?](https://stackoverflow.com/questions/4217510/how-to-cout-the-correct-number-of-decimal-places-of-a-double-value) – AFatBunny Jul 05 '17 at 20:20

2 Answers2

1

In order to print decimals (easily), you need floating point:

printf("%10.6f", static_cast<double>(1) / 3);

If one of the arguments to division is floating point, the compiler will promote the expression to floating point.

Integral or scalar division will lose decimals.

You are always welcome to write your own division function.

Edit 1: Shifting
You don't need to use the pow function (especially since it's floating point).

Use a loop, in the loop multiply your divisor by 10 each time.

double result = (double) scalar / value;
int divisor = 10;
int i;
for (i = 0; i < NUMBER_OF_DECIMALS; ++)
{
  // Isolate a digit using math.
  // print the digit
  divisor += 10;
}

The math part is left as an exercise for the OP.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • thanks for your reply , Floats are not used . since it is the value iam using for Price calculations , so when i used float can't be compared further – 1User Jul 05 '17 at 20:09
  • "%c%5.2f" not allowed – 1User Jul 05 '17 at 20:09
  • Search the internet for "fixed point math c++". You would have better precision with integers. So in $US, one example is to represent number in pennies rather than floating point dollars. Or in length, use millimeters. – Thomas Matthews Jul 05 '17 at 20:11
  • Actually i know to print simple individual numbers into array and display numbers , which requires an array to store the string ..and one temp variable to store the individual value like this . while(num>=1) { temp= temp%10 ; printf("temp",temp) ;num=num/10 ; i++) .........but in my scenario i should print all at once like this ...i should store the value in arr ........printf("%c.%c%c%c%c",value/divisor,arr[0],ar[1],ar[2],ar[3],ar[4]); BUT i am looking for simple logic – 1User Jul 05 '17 at 20:59
  • Please if u understood the problem , can u provide the logic – 1User Jul 05 '17 at 21:04
  • Try developing the logic yourself. Take a number like 3.14159, figure out how to get a digit from it, such as multiplying by some divisor and subtracting from the original number. Trust yourself, or search the internet for "c++ extract decimal digits". – Thomas Matthews Jul 05 '17 at 21:45
0

In this homework exercise you are expected to perform your own number formatting, rather than using that of a library.

For instance, to format and output 100 as "100"

int accum = 100;
int position = 0;
while (accum > 0)
{
   printf("%d", accum % 10);
   accum /= 10;
   position += 1;
}

For your homework assignment, you need to modify the above loop so that it puts a printf(".") in the correct place in the output number. Your answer is likely to involve multiplying accum before the loop and testing position relative to scalar

Tom Leys
  • 18,473
  • 7
  • 40
  • 62