user3213849
user3213849

Reputation: 11

How to set precision to a number

#include <iostream>
#include <math.h>
#include "include/Parser.h"
#include </usr/local/include/mysql++/mysql++.h>
#include "/usr/local/include/mysql++/cmdline.h"
#include </usr/include/mysql/mysql_version.h>
#include "/usr/lib/jsoncpp/include/json/json.h"

int main( int argc, char * argv[] )
{
    Json::Value lat = parser["geo"]["lat"];
    cout << "latitude = " <<lat.toStyledString()<< endl;
}

After passing value to this argument my latitude value is 42.3577770 .I want to make it 42.35 how can I do that?

I tried

cout << setprecision(2) << fixed <<  lat.toStyledString() << '\n';

But it's not working. Is there any other way to do that?

Upvotes: 0

Views: 578

Answers (2)

Frode Nilsen
Frode Nilsen

Reputation: 177

cout << setprecision(2) << fixed << lat.asDouble() << '\n';

(According to online documentation of Json::Value)

Upvotes: 1

Paul Evans
Paul Evans

Reputation: 27567

You want to drop the toStyledString() and work with a float:

cout << setprecision(2) << fixed << lat << '\n';

Upvotes: 0

Related Questions