最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to print numberlong value? - Stack Overflow

programmeradmin0浏览0评论

So, I tried to print a NumberLong to console from a mongoshell. However, when I casted the NumberLong variable into string, the value got rounded down. After initial search, it looks like it happens because the precision limit of javascript float. Here is one example in mongoshell:

var abc = NumberLong(2517720935641120769);
print(abc);

The output is

NumberLong("2517720935641120769")

However, when I tried to cast it into string to just print the value

print(''+abc);

The output is

2517720935641121000

Is there any trick to print the value inside NumberLong?

So, I tried to print a NumberLong to console from a mongoshell. However, when I casted the NumberLong variable into string, the value got rounded down. After initial search, it looks like it happens because the precision limit of javascript float. Here is one example in mongoshell:

var abc = NumberLong(2517720935641120769);
print(abc);

The output is

NumberLong("2517720935641120769")

However, when I tried to cast it into string to just print the value

print(''+abc);

The output is

2517720935641121000

Is there any trick to print the value inside NumberLong?

Share Improve this question edited Nov 13, 2015 at 10:49 mitbal asked Nov 13, 2015 at 10:30 mitbalmitbal 3556 silver badges15 bronze badges 2
  • NumberLong values are essentially "long integers" or 64-bit integers. There is no ponent to be "rounded down" as you claim. The response is likely a Double instead. You basically need to show what you are actually doing and sample data and result for a reasonable question that people can reproduce here as well. Edit your question please. – Blakes Seven Commented Nov 13, 2015 at 10:33
  • See my answer below, you need to quote your numbers when creating a NumberLong otherwise they are converted to floats first: NumberLong("2517720935641120769"); – Alex Commented Nov 13, 2015 at 10:54
Add a ment  | 

2 Answers 2

Reset to default 6

Use the undocumented exactValueString property:

> NumberLong("2517720935641120769").exactValueString
2517720935641120769

Make sure you quote the number when creating a NumberLong field. If you do not quote your value it will be cast to a float and you will loose precision.

NumberLong(123123123123131123).toString()
>>NumberLong("123123123123131120")

However, if you quote your value it will be correctly stored and returned:

NumberLong("123123123123131123").toString()
>>NumberLong("123123123123131123")

Both statements have been executed directly in the MongoShell, the value after >> gives the output returned by the MongoShell (version 3.0.7)

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>